Future University, Faculty of Computers and Information
Technology
Assembly CS223, Fall 2023
Dr. Khaled Ahmed Mohamed
TA: Mayar Sayed Soliman
Assembly Lab - 9
1. Jump
1
// One-Way Selection mov ax, a
if (a > b) { . . . } // if part // One-way selection cmp ax, b
jle end_if
. . . ; if part
end_if:
// Two-Way Selection mov ax, a
if (a <= b) cmp ax, b
{ . . . } // if part jg else_part
else . . . ; if part
{ . . . } // else part jmp end_if
else_part:
. . . ; else part
end_if:
// Short-Circuit AND mov ax, a
if (a > b && a == c) { . . . } // One-way selection cmp ax, b
jle end_if
cmp ax, c
jne end_if
. . . ; if part
end_if:
2
/ Short-Circuit OR mov eax, a
if (a > b || a == c ) { . . . } cmp eax, b
jg if_part
cmp eax, c
jne end_if
if_part:
. . . ; if part
end_if:
// First Translation start_while:
while (a > b) { . . . } mov eax, a
cmp eax, b
jle end_while
. . . ; while body
jmp start_while
end_while:
3
PROGRAM1
This program takes user input, checks if the entered number is divisible by 3, and
outputs a message accordingly.
pseudocode
Prompt user to enter a number
Input the number
Divide the entered number by 3
If remainder is 0:
Display "The given number is divisible by 3"
Else:
Display "The given number is NOT divisible by 3"
End program
4
Code
org 100h ; Origin directive, specifying the starting address of the program
lea dx, msg1 ; Load effective address of msg1 into register dx
mov ah, 9 ; Set interrupt code 9 (display string) into register ah
int 21h ; Call DOS interrupt to display the message
mov ah, 1 ; Set interrupt code 1 (input character) into register ah
int 21h ; Call DOS interrupt to get a single character from the user
mov ah, 0 ; Clear register ah (for division)
mov dx, 0 ; Clear register dx (for division)
mov bx, 3 ; Set divisor to 3
div bx ; Divide the content of the double-word in dx:ax by the operand (3)
cmp dx, 0 ; Compare dx to 0 (remainder after division)
je msgYes ; If dx is 0, jump to label msgYes
lea dx, msg3 ; Load effective address of msg3 into register dx
mov ah, 9 ; Set interrupt code 9 (display string) into register ah
int 21h ; Call DOS interrupt to display the message
jmp exit ; Jump to the exit label
msgYes:
lea dx, msg2 ; Load effective address of msg2 into register dx
mov ah, 9h ; Set interrupt code 9 (display string) into register ah
int 21h ; Call DOS interrupt to display the message
exit:
ret ; Return from the program
msg1 db "Enter any number: $"
msg2 db 10,13,"The given number is divisible by 3$"
msg3 db 10,13,"The given number is NOT divisible by 3$"
5
PROGRAM2
a simple program that takes two single-digit numbers as input, calculates their sum,
and then prints a message displaying the input numbers and their sum.
pseudocode
Define variables:
S1 = "THE SUM OF "
n1 = 0
S2 = " AND "
n2 = 0
S3 = " IS "
n3 = 0
S4 = " $"
Read a single digit from the user and store it in n1
Read a single digit from the user and store it in n2
Calculate the sum of n1 and n2 and store it in n3
Print "THE SUM OF "
Print the value of n1
Print " AND "
Print the value of n2
Print " IS "
Print the value of n3
Print " $"
End program
6
Code
.MODEL SMALL
.STACK 100H
.DATA
S1 DB 0AH,0DH,'THE SUM OF ' ; Message: "THE SUM OF "
n1 DB ? ; Variable for the first input digit
S2 DB ' AND ' ; Message: " AND "
n2 DB ? ; Variable for the second input digit
S3 DB ' IS ' ; Message: " IS "
n3 DB ? ; Variable for the sum
S4 DB ' $' ; Message: " $"
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
; Display a prompt for the first digit
MOV AH,2
MOV DL,'?'
INT 21H
; Read the first digit
MOV AH,1
INT 21H
MOV BL,AL
MOV n1,AL
; Read the second digit
INT 21H
MOV n2,AL
; Calculate the sum of the two digits
ADD BL, AL
SUB BL, 30H
MOV n3, BL
; Display the message "THE SUM OF "
MOV AH,9
LEA DX,S1
INT 21H
; Display the first digit
MOV AH,2
7
MOV DL, n1
INT 21H
; Display the message " AND "
MOV AH,9
LEA DX,S2
INT 21H
; Display the second digit
MOV AH,2
MOV DL, n2
INT 21H
; Display the message " IS "
MOV AH,9
LEA DX,S3
INT 21H
; Display the sum
MOV AH,2
MOV DL, n3
INT 21H
; Display the message " $"
MOV AH,9
LEA DX,S4
INT 21H
; Terminate the program
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
8
Excercise 1
mov ax, 5 ; Set ax to 5
mov bx, 3 ; Set bx to 3
cmp ax, bx ; Compare ax and bx
jg greater ; Jump to label 'greater' if ax is greater than bx
jl lesser ; Jump to label 'lesser' if ax is lesser than bx
jmp equal ; Unconditional jump to label 'equal'
greater:
mov cx, 1 ; Set cx to 1
jmp end ; Unconditional jump to label 'end'
lesser:
mov cx, -1 ; Set cx to -1
jmp end ; Unconditional jump to label 'end'
equal:
mov cx, 0 ; Set cx to 0
end:
mov dx, cx ; Save cx in the dx register