Assembly
Language
Basics
Presented by:
Rajat Chamaria
Saurabh Sharma
Agenda
• Layout of an Assembly Program
• Data Segment
• Intereupts
• Input From Keyboard
– Character, String, and Integer
• Print on Screen
• Working with Turbo Debugger
• Recursion
• Passing Parameters
– By reference
– By value
Layout of an Assembly Program
.model small Types of model :
.stack 100h -small (code-64K,data-64K)
.data -medium (data-64K)
<data here>
-compact (code-64K)
-large (No Limit)
.code
-huge (Same as large)
main proc
mov ax,@data
mov ds,ax
.stack XXXh
<code here>
Where XXX defines
mov ax,4c00h Space allocated on
int 21h stack
main endp
end main
Data segment
• Defines all types of initialized and non-initialized
variables.
– Defining an integar:
A db 20
A dw 5000
A dw ?
– Defining an array:
Arr dw 15 dup(0)
Arr dw 1,3,6,8,9
– Defining a string:
str db “This is a string”,’$’
new db 0dh,0ah,’$’
Interrupts
• int 21h
– Dos interrupt
– Uses function code from ah to specify its action
01h – input character from keyboard
02h – output a character on screen
09h – output a string
4c00h – end processing
Interrupts contd…
• int 00h
– BIOS interrupt
– Invoked by an attempt to Divide by zero
• int 10h
– Used for video input/output
– Clears the screen when ax contains 03h
Inputing data from keyboard
• Input a Character
– Uses subroutine 01h of int 21h
– Value will be stored in AL
mov ah,01h
int 21h
• Input a String
• Input an Integer
Input A String
• Steps to follow
1. Declare a string variable str(say)
2. Load DI with the address of ‘str’ variable
3. Input a character and check for ‘Enter’ key
• If ‘Enter’ is pressed jump to step 6
4. Move this character in memory at [DI]
5. Increment DI and jump to step 3
6. Move ‘$’ sign at [DI] to indicate the end of
string
7. Return to main
Input A String
get_string proc near
lea di,str ;load offset of variable str
next: mov ah,01h ;input a character
int 21h
cmp al,13 ;check for ‘Enter’ key
je str_end
mov [di],al ;Store character in memory
inc di
jmp next ;go for next character
str_end: mov al,24h ;put ‘$’ sign at the end
mov [di],al
ret
get_string endp
Input an Integer
• Steps to follow
1. Clear BX for saving intermediate values
2. Input a character which goes in AL
3. Check for ‘Enter’ key
• Jump to step 7
4. Check for ‘Minus’(-) sign
• Set DI = 1 if Minus key is pressed
5. Subtract 48 to change in numerical value
6. BX=BX*10 + AX and jump to step 2
7. Save in AX from BX
8. Negate the value in AX if Di=1 and return
Input an Integer
get_int: mov di,0 mov digit,ax
mov bx,0 mov ax,bx
mov cx,10 mul cx
get_digit: mov ah,01 add ax,digit
int 21h mov bx,ax
jmp get_digit
cmp al,13
je finish set_neg: mov di,1
jmp get_digit
cmp al,2Dh
je set_neg finish: mov ax,bx
cmp di,1
mov ah,00h je negate
sub ax,30h ret
Print an Integer
• Steps to follow:
1. Check Whether the number is negative
i. Print Minus sign ‘-’ first
ii. Negate the number
2. Set CX=10 and set BX=0 for counting the no.
of digits.
3. Divide AX by CX and Push DX(remainder) on
stack and increment BX until AX=0
4. Output digit by popping in DX and decrement
BX until BX=0
Print An Integer
print_int: mov di,0 was_neg: mov cx,10
rcl ax,1 mov bx,0
jnc not_neg next_digit: mov dx,0
mov di,1 div cx
rcr ax,1
push dx
inc bx
mov bx,ax
lea dx,minus cmp ax,0
call print_msg jne next_digit
mov ax,bx
output: pop dx
call negate call output_digit
jmp was_neg dec bx
cmp bx,0
not_neg: rcr ax,1 jne output
ret
Handover…
• Layout of an Assembly Program
• Data Segment
• Interrupts
• Input From Keyboard
– Character, String, and Integer
• Output
• Working with Turbo Debugger
• Recursion in Assembly
• Passing Parameters
– By reference
– By value
Working with Turbo Debugger
• C:\TASM\BIN> td filename.exe
Program’s Reigon
Register’s Reigon
Flag’s Reigon
Stack’s Reigon
Data Segment
mov ax,5108
mov ds,ax
mov es,ax
mov ax,000A
mov bx,0014
mov cx,001E
mov dx,0028
mov ah,[0001]
mov si,0001
mov ah,4C
mov al,[0000]
int 21h
Recursion
• Function to find the GCD of two numbers
integer GCD(m,n : integer)
{ if n==0
return m;
else
{ remainder=m % n;
return GCD(n,remainder);
print(m,n);
}
}
Calling GCD(m,n)
Addr. Before After
• Passing parameters by
0EC h
value
0EE h
– In this case the values of
0F0 h
parameters are m and n
0F2 h
mov ax,m
0F4 h
mov bx,n
0F6 h
push ax
0F8 h
REG PRE POST push bx
0FA h RET
AX - 20 Call GCD
BX - 15 0FC h n=15
CX - - 0FE h m=20
DX - - 100 h - -
Assembly code of GCD(m,n)
GCD: proceed:
integer GCD(m,n )
pop si mov cx,ax {
pop bx mov dx,0 if n==0
pop ax div bx return m;
Else
push si
{
cmp bx,0 push cx
r=m % n;
push bx return GCD(n,r);
jne proceed
}
ret push bx }
push dx
call GCD 0FA h RET
mov gcd,ax 0FC h n
ret 4
0FE h m RET
100 h - -
Executing
Addr. Before After
GCD: proceed:
pop si mov cx,ax 0EC h
pop bx mov dx,0
0EE h
pop ax div bx
0F0 h
push si
cmp bx,0 push cx 0F2 h
push bx
jne proceed 0F4 h
ret push bx 0F6 h
push dx 0F8 h
REG PRE POST call GCD
0FA h RET
AX 20 20
mov gcd,ax 0FC h n=15
BX 15 15
ret 4
CX - - 0FE h m=20 RET
DX - - 100 h - -
Executing
Addr. Before After
GCD: proceed:
pop si mov cx,ax 0EC h
pop bx mov dx,0
0EE h
pop ax div bx
0F0 h
push si
cmp bx,0 push cx 0F2 h
push bx
jne proceed 0F4 h ret1
ret push bx 0F6 h 5
push dx 0F8 h 15
REG PRE POST call GCD
0FA h 15
AX 20 1
mov gcd,ax 0FC h 20
BX 15 15
ret 4
CX - 20 0FE h RET RET
DX - 5 100 h - -
Executing
Addr. Before After
GCD: proceed:
pop si mov cx,ax 0EC h
pop bx mov dx,0
0EE h
pop ax div bx
0F0 h
push si
cmp bx,0 push cx 0F2 h
push bx
jne proceed 0F4 h ret1
ret push bx 0F6 h 5
push dx 0F8 h 15 ret1
REG PRE POST call GCD
0FA h 15 15
AX 1 15
mov gcd,ax 0FC h 20 20
BX 15 5
ret 4
CX 20 - 0FE h RET RET
DX 5 - 100 h - -
Executing
Addre
Before After
GCD: proceed: ss
pop si mov cx,ax 0EC h
pop bx mov dx,0
0EE h ret2
pop ax div bx
0F0 h 0
push si
cmp bx,0 push cx 0F2 h 5
push bx
jne proceed 0F4 h 5
ret push bx 0F6 h 15
push dx 0F8 h ret1 ret1
REG PRE POST call GCD
0FA h 15 15
AX 15 3
mov gcd,ax 0FC h 20 20
BX 5 5
ret 4
CX - 15 0FE h RET RET
DX - 0 100 h - -
Executing
Addr. Before After
GCD: proceed:
pop si mov cx,ax 0EC h
pop bx mov dx,0
0EE h ret2
pop ax div bx
0F0 h 0
push si
cmp bx,0 push cx 0F2 h 5
push bx
jne proceed 0F4 h 5 5
ret push bx 0F6 h 15 15
push dx 0F8 h ret1 Ret1
REG PRE POST call GCD
0FA h 15 15
AX 3 5
mov gcd,ax 0FC h 20 20
BX 5 0
ret 4
CX 15 - 0FE h RET RET
DX 0 - 100 h - -
Executing
Addr. Before After
GCD: proceed:
pop si mov cx,ax 0EC h
pop bx mov dx,0
0EE h
pop ax div bx
0F0 h
push si
cmp bx,0 push cx 0F2 h
push bx
jne proceed 0F4 h 5
ret push bx 0F6 h 15
push dx 0F8 h Ret1
REG PRE POST call GCD
0FA h 15 15
AX 3 5
mov gcd,ax 0FC h 20 20
BX 5 -
ret 4
CX 15 - 0FE h RET RET
DX 0 - 100 h - -
Executing
Addr. Before After
GCD: proceed:
pop si mov cx,ax 0EC h
pop bx mov dx,0
0EE h
pop ax div bx
0F0 h
push si
cmp bx,0 push cx 0F2 h
push bx
jne proceed 0F4 h
ret push bx 0F6 h
push dx 0F8 h
REG PRE POST call GCD
0FA h 15
AX 3 5
mov gcd,ax 0FC h 20
BX 5 -
ret 4
CX 15 - 0FE h RET
DX 0 - 100 h - -
Passing Parameters by Reference
• void function(int x,int &y, int *z)
{
int b;
b=x;
y=y+b; //y is passed by reference
x=y; //x is passed by value
*z=b;
return;
}
Contd…
mov ax,x ;passing the value of x
lea bx,y ;passing the address of y
push offset z ; var is a dw variable
call function
Contd…
• B=x; -> mov B,ax
• y=y+B; -> mov ax,[bx]
add ax,B
mov [bx],ax
• x=y; -> mov ax,[bx]
Contd…
• *z=b; -> push bp
mov bp,sp
mov di,[bp+4]
mov cx,B
mov [di],cx
STACK
BP
RET MAIN
Offset of Z
-
Thank You