You are on page 1of 18

ASSEMBLY LANGUAGE PROGRAMMING

Format of Assembly Language


[Label] Opcode [Operand1] [Operand2] [; Comment] Assembler C++ i dw ? ; variable definitions int i, j, k; j dw ? k dw ? mov i, 1 i = 1; add i, 4 i = i + 4; mov j, 2 j = 2; mov ax, j k = i + j; add ax, i mov k, ax ; Assembler code on Intel is case-insensitive

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

Named memory locations


BYTE, WORD, DWORD, FWORD, NEAR, FAR, PROC, UNKNOWN Eg., Keybuffer LABEL DB WORD 1, 2

Variables and Memory Allocation


prompt colour j k num lnum db db db db Enter your favourite colour 80 dup (?) 20 ? dw 4000 dd 80000

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

Data Transfer (MOV)


Format: mov <destination , > <source> Source and destination size must be the same Note: Important difference between mov dl, 3 Examples mov mov mov mov and mov dx, 3

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

Input and output


Character output
1. Specify character to be displayed (in DL) 2. Specify DOS I/O sub-program to be used (sub- program no 2h) in AH 3. Interrupt program (int) - transfer control to sub-program specified in AH

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

; All instructions here

.END

Segment directives (Simplified)


DOSSEG .STACK 200h .CODE .DATA Eg., ; Use standard DOS segment ordering convention ; defines a stack 512 bytes long ; marks start of programs code segment ; put immediately before assembler code ; start of data segment must be explicitly loaded into DS!

.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 :

[*] Eg., cmp

means: pointed to by *. byteptr [bx], 0 ; is this the end of string?

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

Basic Operand Types


Operand Type direct direct-offset register-indirect Examples avar avar + 2 [si] [bx] indexed var[bx] [alst + bx] [si + 2] base-indexed [bx + di] [bx][di] [bp-di] base-indexed with [bx + si + 2] displacement var [bx + si] var [bx][si] Description EA is the offset of a variable EA is sum of variable's offset and displacement EA is the contents of a base or index register EA is the sum of a base or index register and a displacement EA is the sum of a base register and an index register EA is the sum of a base register, an index register, and a displacement

You might also like