Bismillahir Rahmanir Rahim
At first read this topic: Some statement and segment to write Assembly program
Now it will be easy to understand how the code is writing lets do writing
there are some steps to write a assembly:
a) Declare and define data
b) Declare and define stack
c) Declare and define instruction code
b) Declare and define stack
c) Declare and define instruction code
- • Instruct the assembler to use a logical segment as physical segment
- • Get the segment base number of data and stack segment in DS and SS
- registers
- • Write the data processing part of program
• Do arithmetic / logic operation on data
• Copy results from CPU registers to memory
- • Call the Operating System to carry out input / output job (Optional)
- • Call the Operating System to terminate the program
C/C++ program:
#include <stdlib.h>
void main()
{
int aa = 05, bb = 04, sum ;
sum = aa + bb ;
printf(”%d”, sum );
}
;; definition of logical data segment datas SEGMENT aa DB 05h bb DB 04h sum DB ? datas ENDS |
SEGMENT- ENDS : define a logical segment in the memory. The starting memory location is given some name, used for reference. DB: (define byte) reserve a byte size memory , initialize it with a byte size number or leave it uninitialized (?). The memory location may be named and used as variable |
;; definition of logical stack segment stacks SEGMENT DB 100 DUP(0) stacks ENDS |
n DUP(m): reserve ‘n’ byte size memory locations, initialize it with a byte size number or leave it uninitialized (?). The memory location is not named |
;; definition of logical code segment codes SEGMENT ASSUME CS: codes, DS: datas, SS: stacks start: ;; beginning address of program ({) MOV ax, SEG datas MOV ds, ax MOV cx, SEG stacks MOV ss, cx ;; data processing MOV al, aa MOV cl, bb ADD al, cl MOV sum, al ;; display result on monitor ADD al, 30h MOV dl, al MOV ah, 02h INT 21h ;; terminate program MOV ax, 4C00h INT 21h codes ENDS END start ;; end of program (}) |
ASSUME: declare which logical segment will be used as physical segments – code, data and stack segments Initialization of Segments registers When the operating system loads a program into memory for execution • The OS loads the CS segment register only with segment base number. • The DS, SS segment register is to be loaded with segment base number by programmer. SEG : load a register with segment base number. • Data can be copied into segment registers through general purpose register only Operating system call for Input / output • Convert the binary number into ASCII by adding 30h • Put the data in DL reg • Put function number = 02h to display character in AH reg • OS call to branch to subroutine, whose address is in Interrupt vector table at location 0084h from base of memory System call for return to operating system • Put function number = 4Ch in AH reg • Put 00h in AL reg for normal termination • OS call to branch to subroutine, whose address is in Interrupt vector table at location 0084h from base of memory |
Now you can read this topic: Save, Compile and Debugging a assembly progra
Reference:
Dr Ehteshamul Haque (lecture)
---------HAPPY CoDing---------