Professional Documents
Culture Documents
Data Initialization
DB DW DD 1 byte 2 bytes = one word 4 bytes = one doubleword DF, DQ DT 6 bytes = one far ptr (386) 8 bytes = one quad word 10 bytes
Initializing arrays
Example SampleArr DW 0, 1, 2, 3, 4
Constants
CONSTANT_NAME CR equ 13D LF equ 10D BELL equ 7D equ definition
Comments
; A comment always starts with a ; mov j, 1 ; it can also follow an instruction
bx, 65D bx, 41H ; Can also mov hexadecimal numbers: bx, 01000001B ; and binary numbers al, bh ; register sizes must be the same
Arithmetic instructions
Formats: add reg_mem, mem_reg sub reg_mem, mem_reg inc reg_mem dec reg_mem mul reg_mem ; other operand MUST be al or ax. ; 8 bit result in ax, 16 bit result in dx:ax div reg_mem ; divide ax by reg_mem value. ; 8 bit quotient in al, remainder in ah ; 16 bit quotient ix ax, remainder in dx ; reg_mem, mem_reg are register of memory references Cannot add or mov two memory variables: add k, i ; This is ILLEGAL!
Terminating a program
mov int ax, 4c00h 21h ; load ax with pointer to terminate program ; call DOS interrupt function
Character input
1. Load AH register with read character sub program number 2. Use int to call DOS to perform input. Character from keyboard will be in AL register Character output mov dl, a mov ah, 2h int 21h Character input mov ah, 1h int 21h mov ch, al
String output
1. Specify strings address in DX register. Ensure that its terminated by a $ ! 2. Specify string output sub-program by storing 9h in AH register 3. Use int 21h to call DOS execute of sub-program 9h
String input
Use character input routine in a loop
Program skeleton
.MODEL SMALL .STACK 200h .DATA MemVar DW : .CODE : ; Can be upper- or lowercase ; Most assembler directives start with a . ; All data allocation instructions here
.END
.DATA Top DW 100 ErrMsg DB Oops, I think you made an error, $ DOSSEG Segments grouped according to Microsoft ordering conventions .MODEL Specifies memory model for assembler routine tiny: Both code and data must fit in 64 Kb segment small Code in 64Kb segment and data in separate segment medium Code larger than 64 Kb (far), but data not (near) large Both code and data far, but arrays < 64 Kb
Sub- programs
Example:- Call the sub-program add_regs: mov cx, 10 ; set up registers cx and bx mov bx. 15 call add_regs ; carries out ax = bx + cx mov sum, ax ; sum = ax add_regs: mov add add ret Adds cx to bx and returns result in ax ax, 0 ; make sure ax is empty ax, bx ax, cx ; ax will contain bx + cx
Conditional Statements
1. Evaluate condition eg., ax = bx 2. Transfer control to appropriate point (label) depending on outcome Example: if (i = = j) cmp ax, bx ; compare ax and bx { jne eq_lab ; if ax != bx go to eq_lab i = i + 10; mov ax, i j = j - 10; mov bx, j } add ax, 10 ; else, ax = ax + 10 else sub bx, 10 ; and bx = bx - 10 { eq_lab: ; label for jump destination i = i + 2; add ax, 2 ; ax = ax + 2 (mistake here?) j = j - 2; sub bx, 2 ; bx = bx - 2 } ; Use jmp instead of je or jne for unconditional branch
Iteration
Example i=; /* loop counter sum = 0; while ( i != 21) { sum = sum + i; i + i + 1; } /* sum now contains result ; Assume sum and i have been defined mov sum, 0 mov cx, 1 while1: cmp cx, 21 je finito add sum, cx ;sum = sum + cx inc cx jmp while1 finito: ; sum now contains the result
Indirect addressing
bx 1024 : 1024 1025 1026 1027 1028 1029 :
offset - allows us to specify address of a memory variable Eg., mov bx, offset message ; means: move address of variable message to bx
H e l l o 0
Example: Count number of chars in string terminated by NULL character (ASCII 0) message db Hello, 0 : : mov cx, 0 ; cx stores number of characters mov bx, offset message ; store address of message in bx start: cmp byteptr [bx], 0 ; is this the end of string? je Finished ; if so, go to Finished inc cx ; increment cx inc bx ; bx points to next character jmp start ; cx now contains number of characters in message : Finished:
Indexed addressing
Is like subscripted addressing of arrays Example msg : mov mov start: cmp je inc inc jmp fin db Hello, 0 cx, 0 bx, 0 msg [bx], 0 fin bx cx start ; cx = 0 ; bx = 0 (index register)
Stack addressing
Is used transparently by program Stack pointer (sp) register tracks last element on stack. Stack operations: push and pop Eg., push ax ; pushes value of ax onto stack and increments sp pop ax ; pops value from stack into ax, decrements sp On sub-program calls, the return address of sub- program placed on stack by call instruction. The ret instruction retrieves return address from stack. Example push push pop pop
ax bx ax bx
; store ax on stack ; store bx on stack ; copy last value to ax ; copy first value to bx