Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

DEPARTMENT OF COMPUTER SCIENCE

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, MARDAN

LAB MANUAL
CS203-COMPUTER ORGANIZATION AND ASSEBMLY LANGUAGE
BSCS, 3rd Semester, Section B, Fall 2023
Course Instructor: Dr. Tariq Sadad

##########################################################
LAB 01 LECTURE
MASM INSTALLATION, AL PROGRAM STRUCTURE, BASICS CONCEPTS

Install DOSBOX from website: http://programologysofts.blogspot.com/


Type DOSBOX in the search area and click Download.
Copy MP folder to C drive.
Then open the DOSBOX from the shortcut on the desktop, and type the
following command in the command prompt.

mount c c:\mp
c:
c:\>edit first.asm

And save the file from File menu

Then type the following program:

PROGRAM: AL program to print a single character, first.asm

dosseg

.model small
.stack 100h
.data
.code
main proc

mov dl, ‘f’


mov ah,2
int 21h
mov ah, 4ch
int 21h
main endp
end main

Steps for executing the AL program using DOSBOX


Assemble: masm first.asm
Link: first.obj
Execute: first.exe

Addressing modes, mov instruction service routine, ASCII code and


Interrupt

Addressing mode: Ways or models to access data


Register addressing: When both operands are registers
Immediate addressing: one register and a constant value
Memory addressing: register and a variable, e.g. add dl, [address]
Destination must be register or a memory address.

Data transfer instruction


mov dl, 2
mov dl,’A’

for printing send a command to AX


mov ah, 2 ; 2 as a function / service routine
int 21h ; moving out of the register

Service Routines for Interrupts


1 = input a character with echo
2 = output/print a single character ‘a’
8 = input a character without echo
9 = print collection of characters ‘abcd’
4ch = exit

Interrupt
Stop the current program and allow the microprocessor to access
hardware to take input or give output.

int 21h ; interrupt for text handling


int 20h ; interrupt for video/graphics handling

ASCII Code
When output is not exact, and it is a smiley face or something else
that is actually ascii code.

Character Encoding Scheme


A=65
B=66…
a=97
b=98…

next line feed 10


carriage return 13

----------------------------------------------------------------

LAB 01 TASKS

LAB TASK 1: Download and install DOSBOX on lab computers and execute
your first AL program? Write the program execution steps?
LAB TASK 2: Write an AL program to print your university registration
number?

################################################
################################################

LAB 02 LECTURE
Install VSCDOE from website
https://code.visualstudio.com/

Program: Print your name using separate characters: name.asm

dosseg

.model small
.stack 100h
.data
.code
main proc

mov dl, 'k'


mov ah,2
int 21h

mov dl,'h'
mov ah,2
int 21h

mov dl,'a'
mov ah,2
int 21h

mov dl,'n'
mov ah,2
int 21h

mov ah,4ch
int 21h

main endp
end main

OUTPUT: khan

LAB 03 LECTURE
REGISTERS, USER INPUT, ADDITION, SUBTRACTION

Program: input a char from user and print it: charinpu.asm

dosseg

.model small
.stack 100h
.data
.code
main proc

mov ah,1
int 21h

mov dl,al
mov ah,2
int 21h

mov ah,4ch
int 21h

main endp
end main

-------------------------------------------

Program: Subtracting two numbers: subnum.asm

dosseg

.model small
.stack 100h
.data
.code
main proc

;addressing mode
mov bl,3
mov cl,1
sub bl,cl

add bl,48

mov dl,bl
mov ah,2
int 21h

mov ah,4ch
int 21h

main endp
end main

--------------------------------------

Program: adding to numbers: addnum.asm

dosseg

.model small
.stack 100h
.data
.code
main proc
mov bl, 1
mov cl,2
add bl,cl
add bl,48
mov dl,bl
mov ah,2
int 21h

mov ah,4ch
int 21h

main endp
end main

LAB 3 TASKS

LAB TASK 1: Add and subtract two numbers in a single AL program?


LAB TASK 2: Write a program to get input of multiple characters from
user and print it?
################################################
################################################
LAB 04 LECTURE
DATA TYPES IN AL, OFFSET

Variable, data types and offset

mov ax, @data


It moves the memory location of @data into the AX register.

mov dx,ax
moves data address to ds so that the data segment gets initialized as
heap memory to access variables fast.

Type mismatch
mov dx,var1 ; is incorrect if var1 size is 8bits.

Offset: holds the beginning address of variables as 16 bits.


mov dx, offset var3

Or

lea dx,var3
Load Effective Address: it is an indirect addressing instruction used
as a pointer in which first variable points the address of second
variable.

Program: Variable, data types and offset: vardt.asm

dosseg

.model small
.stack 100h
.data
var1 db '1'
var2 db ?
var3 db '12345$'
.code
main proc

mov ax,@data
mov ds,ax

mov dl,var1
mov ah,2
int 21h

;mov dl,var2
;mov ah,2
;int 21h

;mov dl,var3 this is incorrect; first char from string

mov dx,offset var3


;lea dx,var3

mov ah,9
int 21h

mov ah,4ch
int 21h

main endp
end main

------------------------------------------------

Program: print two string in two different lines, stringlab.asm

Reminder: Print $ at the end of each string.

dosseg

.model large
.stack 100h
.data
msg1 db 'hello$'
msg2 db 'world$'

.code
main proc

mov ax,@data
mov dx,ax

mov dx,offset msg1


mov ah,9
int 21h

mov dx,10 ; new line


mov ah,2
int 21h

mov dx,13 ; carriage return


mov ah,2
int 21h
;now we are on the second line
mov dx,offset msg2
mov ah,9
int 21h

;now going out of the register for display


mov ah,4ch
int 21h

main endp
end main

LAB 4 TASKS

LAB TASK 1: Write an AL program by using different data types: DB, DW,
DD and print the different type of values.
LAB TASK 2:

################################################
################################################

LAB 5 LECTURE
JUMP AND LOOP INSTRUCTIONS

PROGRAM: using LOOP, print 0-9 numbers in ascending order

dosseg

.model small
.stack 100h
.data
.code
main proc

mov cx,10
mov dx,48 ;ascii code for 0

test1:
mov ah,2 ;print
int 21h ;interrupt to go out

add dx,1
;inc dx
loop test1
mov ah,4ch
int 21h

main endp
end main
ed

---------------------------------------------

PROGRAM: comparing two numbers using CMP, cmpnum.asm

dosseg

.model small
.stack 100h

.data
msg1 db 'numbers are equal$'
msg2 db 'numbers are not equal$'
.code
main proc

mov ax,@data
mov ds,ax

mov dl,'3' ;store ascii code of 3


mov ah,1 ; taking input
int 21h

;comparing
cmp al,dl

je l1

mov dx,offset msg2


mov ah,9
int 21h
mov ah,4ch
int 21h

l1:
mov dx,offset msg1
mov ah,9
int 21h

mov ah,4ch
int 21h

main endp
end main
LAB 5 TASKS

LAB TASK 1: Write an AL program to print the numbers 9 – 0 in


descending order.
LAB TASK 2: Declare a variable containing the word “TEST”, write a
program to print it 5 times, repeatedly.

################################################
################################################

LAB 6 LECTURE
ARRAY, DUP, SOURCE INDEX

PROGRAM: Simple array, arraylab1.asm

dosseg

;if prog size is < 64KB then small model


.model small
.stack 100h

.data
arr1 db 1h,2h,3h,4h

.code
main proc
mov ax,@data
mov ds,ax

mov si,offset arr1


mov dx,[si]
mov ah,2
int 21h

;mov dx,[si+1]
inc si
mov dx,[si]
mov ah,2
int 21h

inc si
mov dx,[si]
mov ah,2
int 21h

inc si
mov dx,[si]
mov ah,2
int 21h

mov ah,4ch
int 21h

main endp
end main

---------------------------------------------

PROGRAM: Print an array using loop; arrlop.asm

dosseg

.model small
.stack 100h

.data
arr db 'a','b','c'

.code
main proc

mov ax,@data
mov ds,ax

mov si,offset arr

mov cx,3

l1:
mov dx,[si]

mov ah,2
int 21h

inc si
loop l1

mov ah,4ch
int 21h

main endp
end main

LAB 6 TASKS

LAB TASK 1: Write an AL program to print an array value on a specific


index, for example print an array value on index 2.
LAB TASK 2: Using two different types of array, and print its values.

################################################
################################################

LAB 07 LECTURE
STRING, JUMP INSTRUCTIONS

Program to input String from user and print its length

.model small
.stack 100h
.data
var1 db 100 dup("$")

.code
main proc
mov ax,@data
mov ds,ax

mov bl, 0 ; counts the length of string


mov si,offset var1
l1: mov ah,1
int 21h

cmp al,13
je printString
mov [si],al
inc si
inc bl
jmp l1
printString:
mov dl, bl

mov ah,2
add dl, 48
int 21h

mov ah,4ch
int 21h

main endp
end main

----------------------------------

Program: Get an integer from user and display whether the number is
even or odd.

.model small
.stack 100h
.data
ev db 'Even$'
od db 'Odd$'
.code
main proc
mov ax,@data
mov ds,ax

mov ah,1
int 21h

mov bl,2
div bl
cmp ah,0
je IsEven

mov dx,10
mov ah,2
int 21h
mov dx,13
mov ah,2
int 21h

mov dx,offset od
mov ah,9
int 21h

mov ah,4ch
int 21h

IsEven:
mov dx,10
mov ah,2
int 21h
mov dx,13
mov ah,2
int 21h

mov dx,offset ev
mov ah,9
int 21h

mov ah,4ch
int 21h

main endp
end main

---------------------------------------

PROGRAM: Display whether the given number is divisible by 2 or 3

.model small
.stack 100h

.data
ev db 'divisible by 2$'
od db 'divisible by 3$'

.code
main proc
mov ax,@data
mov ds,ax
mov ah,1
int 21h

mov bl,2
div bl

cmp ah,0
je IsEven

mov dx,10
mov ah,2
int 21h
mov dx,13
mov ah,2
int 21h

mov dx,offset od
mov ah,9
int 21h

mov ah,4ch
int 21h

IsEven:
mov dx,10
mov ah,2
int 21h
mov dx,13
mov ah,2
int 21h

mov dx,offset ev
mov ah,9
int 21h

mov ah,4ch
int 21h

main endp
end main

LAB 7 TASKS

LAB TASK 1: Store a string in a variable and print its length?


LAB TASK 2: Get an integer from user and display whether the number
is a prime number or not?

################################################
################################################
LAB 08 LECTURE
STRINGS, LOOP

Program to input a string from user and print it; strdup.asm

dosseg
.model small
.stack 100h

.data
var1 db 20 dup('$')

.code
main proc
mov ax,@data
mov ds,ax

mov si,offset var1

l1:
;taking input from user
mov ah,1
int 21h

cmp al,13 ; 13 is ascii code for enter key


je progend

mov [si],al
inc si
jmp l1

progend:
mov dx,offset var1
mov ah,9
int 21h

mov ah,4ch
int 21h

main endp
end main

-----------------------------------------

PROGRAM: write a program to print A to Z alphabets in capital letters

dosseg
.model small
.stack 100h
.data
.code
main proc
mov cx,26
mov ah,2
mov dl,65

l1:
int 21h
inc dl
loop l1

mov dl,10
mov ah,2
int 21h

mov dl,13
mov ah,2
int 21h

mov cx,26
mov dl,97
mov ah,2

l2:
int 21h
inc dl
loop l2

mov ah,4ch
int 21h

main endp
end main

LAB 8 TASKS

Lab Task 1: Write a program to print A to Z alphabets in small letters


Lab Task 2: Write a program to print your name on the screen, by
taking input from the user?

################################################
################################################

LAB 09 LECTURE
ADDRESSING MODES, MEMORY MODELS
PROGRAM: find the largest number in an array, lrgnum.asm

.model small
.stack 100h

.data
STRING1 DB 1,3,8,5
res db ?

.code
main proc

mov ax,@data
mov ds,ax
mov cx, 4
mov bl, 0
LEA SI, STRING1

up:
mov al, [SI]
cmp al, bl
jl nxt

mov bl, al
nxt:
inc si
dec cx
jnz up

mov res,bl
mov dl,res
add dl,48
mov ah,2
int 21h

mov ah,4ch
int 21h
main endp
end main

-----------------------------------------

PROGRAM: taking input from keyboard and add them

dosseg

.model small
.stack 200
.data

crlf db 0Dh,0Ah,'$'
prompt1 db 'enter the first number$'
prompt2 db 'enter the second number$'
prompt3 db 'the sum of two numbers is: $'

.code
main proc
lea dx,prompt1
mov ah,09h
int 21h

mov ah,01h
int 21h

sub al,30h
mov cl,al
lea dx,crlf
mov ah,09h
int 21h

lea dx,prompt2
mov ah,09h
int 21h

mov ah,01h
int 21h
sub al,30h
add al,cl

mov cl,al
add cl,30h

lea dx,crlf
mov ah,09h
int 21h

lea dx,prompt3
mov ah,09h
int 21h

mov dl,cl
mov ah,02h
int 21h

mov ah,04ch
int 21h

main endp
end main

LAB 9 TASKS

Lab Task 1: Find the smallest number in an array


Lab Task 2: taking input of two numbers from keyboard and subtract
them

LAB 10 LECTURE
STACK AND ITS OPERATIONS
PROGRAM: PUSH and POP operations in stack. stack1.asm

dosseg

.model small
.stack 100h
.data
.code
main proc
mov ax,'3'
push ax

mov bx,'5'
push bx

mov dx,ax
mov ah,2
int 21h

mov dx,bx
mov ah,2
int 21h

mov ah,4ch
int 21h

main endp
end main

----------------------------------------

Program: swapping two numbers using stack swp1.asm

dosseg

.model small
.stack 100h

.data
.code
main proc

mov ax,'4'
push ax ;copies 4 to stack

mov bx, '7'


push bx ;copies 7 to stack

pop ax ;moves 7 from stack to ax


pop bx ;moves 4 from stack to bx

mov dx,ax
mov ah,2
int 21h

mov dx,bx
mov ah,2
int 21h

mov ah,4ch
int 21h

main endp
end main

------------------------------------------

PROGRAM: Program to reverse string using stack, strev1.asm

dosseg

.model small
.stack 100h
.data
string db 'ali'
.code
main proc
mov ax,@data
mov ds,ax
mov si,offset string
mov cx,3

;loop for pushing values to stack


l1:
mov bx,[si]
push bx
inc si
loop l1

mov cx,3 ;counter to pop values


l2:
pop dx
mov ah,2
int 21h
loop l2

mov ah,4ch
int 21h

main endp
end main
LAB 10 TASKS

LAB TASK 1: Write a program to reverse the input string from keyboard
LAB TASK 2: Write a program to reverse the input numerical values from
keyboard
LAB TASK 3: Program: Jump conditions -> to input two numbers and check
if they are equal, unequal, greater or lesser.

################################################
################################################

LAB 11 LECTURE
STACK, NESTED LOOP

PROGRAM: assembly program to print the following pattern, patrn.asm

*
**
***
****
*****

dosseg

.model small
.stack 100h

.data
.code
main proc

mov ax,@data
mov ds,ax

mov bx,1 ;inc bx in each outer loop

mov cx,5
L1:
push cx ;to keep the outer 5 reserved

mov cx,bx ;if not stack, here cx will be overided


L2:
mov dl,'*'
mov ah,2
int 21h
loop L2

mov dl,10
mov ah,2
int 21h

mov dl,13
mov ah,2
int 21h

;loop L2

inc bx
pop cx ;receive the cx back
loop L1

mov ah,4ch
int 21h

main endp
end main

LAB 11 TASKS

LAB TASK 1: Write an assembly program to print the following pattern.

1
22
333
4444
55555

LAB TASK 2: Write an assembly program to print the following pattern.


55555
4444
333
22
1

LAB TASK 3: Write an assembly program to print the following pattern.


*****
****
***
**
*

################################################
################################################
LAB 12 LECTURE
Procedures and Macros

PROGRAM: Write a program to create a simple procedure in AL, proc1.asm

dosseg

.model small
.stack 100h
.data
str1 db 'hello$'
str2 db 'how are you$'
str3 db 'good to see you$'

.code
main proc
mov ax,@data
mov ds,ax

mov dx,offset str1


mov ah,9
int 21h

call enterkey

mov dx,offset str2


mov ah,9
int 21h

call enterkey

mov dx,offset str3


mov ah,9
int 21h

mov ah,4ch
int 21h

main endp

enterkey proc
mov dx,10
mov ah,2
int 21h

mov dx,13
mov ah,2
int 21h
ret
enterkey endp
end main

--------------------------------------

PROGRAM; write an AL program to create a macro, macr1.asm

dosseg

print macro p1
mov dx,offset p1
mov ah,9
int 21h
endm

.model small
.stack 100h
.data
str1 db 'hello$'
str2 db 'test program for macro$'

.code
main proc
mov ax,@data
mov ds,ax

print str1
print str2

mov ah,4ch
int 21h

main endp
end main

LAB 12 TASKS

LAB TASK 1: Write a program to add two numbers using procedure /


subroutine?
LAB TASK 2: Write a program to subtract two numbers using procedure /
subroutine?
LAB TASK 3: Write a program using both procedures and macros to print
three different strings in three separate lines?
LAB TASK 4: Write a program to take string input from user and print
it again using procedures and macros?

################################################
################################################
LAB 13 LECTURE
DIVISION AND MULTIPLICATION USING DIV AND MUL

PROGRAM: divide two numbers and print quotient and remainder

dosseg

.model small
.stack 100h

.data
quo db ?
rem db ?
.code
main proc

mov ax,26
mov bl,5
div bl

mov quo,al
mov rem,ah

mov dl,quo
add dl,48
mov ah,2
int 21h
mov dl,rem
add dl,48
mov ah,2
int 21h

mov ah,4ch
int 21h

main endp
end main

---------------------------------

PROGRAM: write a program to multiply two numbers

dosseg

.model small
.stack 100h

.data
.code
main proc
mov al,5
mov bl,2
mul bl
AAM
mov ch,ah ;1 in ah moves to ch
mov cl,al ;0 in al moves to cl

mov dl,ch ;1 to dl
add dl,48 ;convert to ascii to ready for print
mov ah,2
int 21h

mov dl,cl ;0 to dl
add dl,48
mov ah,2
int 21h

mov ah,4ch
int 21h

main endp
end main

--------------------------

LAB 13 TASKS

LAB TASK 1: Write a program to take input from user and divide two
numbers using procedure. Required output template is as below.

Enter Numerator:
Enter Denominator:

Quotient:
Remainder:

LAB TASK 2: Program to take input two numbers and multiply them, using
procedure. Output should look like as below.

Enter first number:


Enter second number:

Product is:

################################################
################################################
LAB 14 LECTURE
Shift and Rotate Instructions

PROGRAM: Write a program to perform one bit shift left.

dosseg
.model small
.stack 100h
.data

.code
main proc

mov ax, 2
shl ax, 1
add ax, 48
mov dx, ax
mov ah, 2
int 21h

mov ah, 4ch


int 21h

main endp
end main
-----------------------------

PROGRAM: Write a program to perform one bit rotate right.

dosseg
.model small
.stack 100h
.data

.code
main proc
mov ax, 6
ror ax, 1
add ax, 48
mov dx, ax
mov ah, 2
int 21h

mov ah, 4ch


int 21h

main endp
end main
------------------------------

LAB 14 TASKS

Task 1: Write a program to take input from the user and perform two
bits shift left.

Task 2: Write a program to take input from the user and perform three
bits rotate right.

Task 3: Using procedure, write a program to take input from the user
and perform two bits rotate left.

################################################
################################################

LAB 15 LECTURE
Addressing Modes: Instructions only

MOV Instruction rules


mov reg, reg
mov mem, reg
mov reg, mem
mov mem, imm
mov reg, imm
mov r/m16, sreg
mov sreg, r/m16

------------------------
MOVZX

mov bl, 8Fh


movzx ax, bl

----------------------
MOVSX

mov bl, 8Fh


movsx ax, bl

-------------------
XCHG

.DATA
var1 DWORD 10000000h
var2 DWORD 20000000h
.CODE
xchg ah, al ; exchange 8-bit regs
xchg ax, bx ; exchange 16-bit regs
xchg eax, ebx ; exchange 32-bit regs
xchg var1,ebx ; exchange mem, reg

--------------------------
Direct Memory Operands

.DATA
var1 BYTE 10h
.CODE
mov al, var1 ; AL = var1 = 10h
mov al,[var1] ; AL = var1 = 10h
--------------------------
Direct-offset operand

.DATA
arrayW WORD 1020h, 3040h, 5060h
arrayD DWORD 1, 2, 3, 4
.CODE
mov ax, arrayW+2
mov ax, arrayW[4]
mov eax,[arrayD+4]

-----------------------------

Direct Memory Addressing

.data
var1 DWORD 100
var2 DWORD 200
sum DWORD ?
.code
mov eax, var1
add eax, var2
mov sum, eax

-------------------------
Register Indirect Addressing
.data
array DWORD 10000h,20000h,30000h
.code
mov esi, OFFSET array ; esi = array address
mov eax,[esi] ; eax = [array] = 10000h
add esi,4 ; why 4?
add eax,[esi] ; eax = eax + [array+4]

-----------------------------
Index Addressing
.data
array DWORD 10000h,20000h,30000h
.code
mov esi, 0 ; esi = array index
mov eax,array[esi] ; eax = array[0] = 10000h
add esi,4
add eax,array[esi] ; eax = eax + array[4]

------------------------------

Index Scaling

.DATA
arrayB BYTE 10h,20h,30h,40h
arrayW WORD 100h,200h,300h,400h
arrayD DWORD 10000h,20000h,30000h,40000h
.CODE
mov esi, 2
mov al, arrayB[esi] ; AL = 30h
mov ax, arrayW[esi*2] ; AX = 300h
mov eax, arrayD[esi*4] ; EAX = 30000h

-------------------------
Base Addressing

.DATA
mystruct WORD 12
DWORD 1985
BYTE 'M'
.CODE
mov ebx, OFFSET mystruct
mov eax, [ebx+2] ; EAX = 1985
mov al, [ebx+6] ; AL = 'M'

-------------------------

Based Indexed Addressing

.data
matrix DWORD 0, 1, 2, 3, 4 ; 4 rows, 5 cols
DWORD 10,11,12,13,14
DWORD 20,21,22,23,24
DWORD 30,31,32,33,34
ROWSIZE EQU SIZEOF matrix ; 20 bytes per row
.code
mov ebx, 2*ROWSIZE ; row index = 2
mov esi, 3 ; col index = 3
mov eax, matrix[ebx+esi*4] ; EAX = matrix[2][3]

---------------------------------------
LAB 15 TASKS

LAB TASK 1: Write AL instructions for:


 Register Indirect Addressing using BYTE data type.
 Indexed addressing using WORD data type.
 Based Indexed using WORD data type.

################################################
################################################
LAB 16 LECTURE
Conditional Jumps Review, While loop, Switches

Conditional Jump Instructions


Conditional jump instructions can be divided into four groups:
• Jumps based on the value of a single arithmetic flag
• Jumps based on the value of CX or ECX
• Jumps based on comparisons of signed operands
• Jumps based on comparisons of unsigned operands

The following is a list of jumps based on the Zero, Carry,


Overflow, Sign, and Parity flags.

Mnemonic Description Flags


JZ, JE Jump if Zero, Jump if Equal ZF = 1
JNZ, JNE Jump if Not Zero, Jump if Not Equal ZF = 0
JC Jump if Carry CF = 1
JNC Jump if No Carry CF = 0
JO Jump if Overflow OF = 1
JNO Jump if No Overflow OF = 0
JS Jump if Signed (Negative) SF = 1
JNS Jump if Not Signed (Positive or Zero) SF = 0
JP, JPE Jump if Parity, Jump if Parity is Even PF = 1
JNP, JPO Jump if Not Parity, Jump if Parity is Odd PF = 0
The following table shows the jumps based on the value of CX and ECX:

Mnemonic Description
JCXZ Jump if CX = 0
JECXZ Jump if ECX = 0

The following table shows a list of signed jumps based on comparisons of signed operands:
Mnemonic Description Condition Tested
JG, JNLE Jump if Greater, Jump if Not Less or Equal ZF = 0 and SF = OF
JGE, JNL Jump if Greater or Equal, Jump if Not Less SF = OF
JL, JNGE Jump if Less, Jump if Not Greater or Equal SF ≠ OF
JLE, JNG Jump if Less or Equal, Jump if Not Greater ZF = 1 or SF ≠ OF

The following shows a list of unsigned jumps based on comparisons of unsigned operands:

Mnemonic Description Condition Tested


JA, JNBE Jump if Above, Jump if Not Below or Equal ZF = 0 and CF = 0
JAE, JNB Jump if Above or Equal, Jump if Not Below CF = 0
JB, JNAE Jump if Below, Jump if Not Above or Equal CF = 1
JBE, JNA Jump if Below or Equal, Jump if Not Above ZF = 1 or CF = 1
Block-Structured if statement

mov eax,var1
if( var1 == var2 ) cmp eax,var2
X = 1; jne elsepart
else mov X,1
X = 2; jmp next
elsepart:
mov X,2
next:

mov eax,var1
if (var1 <= var2) { cmp eax,var2
var3 = 10; jle ifpart
} mov var3,6
else { mov var4,7
var3 = 6; jmp next
var4 = 7; ifpart:
} mov var3,10
next:

Compound Expression with AND

; One Possible Implementation ...


if ((al > bl) && cmp al, bl ; first expression ...
(bl > cl)) {X = 1;} ja L1 ; unsigned comparison
jmp next
L1: cmp bl,cl ; second expression ...
ja L2 ; unsigned comparison
jmp next
L2: mov X,1 ; both are true
next:

if ((al > bl) && cmp al,bl ; first expression...


(bl > cl)) {X = 1;} jbe next ; quit if false
cmp bl,cl ; second expression...
jbe next ; quit if false
mov X,1 ; both are true
next:
Compound Expression with OR

cmp al,bl ; is AL > BL?


if ((al > bl) || (bl ja L1 ; yes, execute if part
> cl)) {X = 1;} cmp bl,cl ; no: is BL > CL?
jbe next ; no: skip if part
L1: mov X,1 ; set X to 1
next:

WHILE Loops

top: cmp eax,ebx ; eax < ebx ?


while( eax < ebx) { eax = jae next ; false? then exit loop
eax + 1; } inc eax ; body of loop
jmp top ; repeat the loop
next:
while (ebx <= var1) { top: cmp ebx,var1 ; ebx <= var1?
ebx = ebx + 5; ja next ; false? exit loop
var1 = var1 - 1 add ebx,5 ; execute body of loop
} dec var1
jmp top ; repeat the loop
next:
Switch (ch) { case0:
case '0': exit(); exit
case '1': count++; case1:
break; inc count
case '2': count--; jmp exitswitch
break; case2:
case '3': count += 5; dec count
break; jmp exitswitch
case '4': count -= 5; case3:
break; add count, 5
default : count = 0; jmp exitswitch
} case4:
sub count, 5
jmp exitswitch
default:
mov count, 0
exitswitch:

Understand the difference between iterative techniques and recursive


techniques

Recursion and iteration are both different ways to execute a set


of instructions repeatedly. The main difference between these two
is that in recursion, we use function calls to execute the
statements repeatedly inside the function body, while in
iteration, we use loops like “for” and “while” to do the same.

Iteration is faster and more space-efficient than recursion. So


why do we even need recursion? The reason is simple — it's easier
to code a recursive approach for a given problem.

Note: So the topic Procedure is an example of recursion and the loops


for, while) are the examples of iterative techniques.

LAB 15 TASKS

LAB TASK 1: Write AL code for the following:

if( ebx <= ecx )


{
eax = 5;
edx = 6;
}

LAB TASK 2: Write AL code for the following:

if ((ebx <= ecx) && (ecx > edx))


{
eax = 5;
edx = 6;
}

LAB TASK 3: Write AL code for the following:

while (ebx <= var1) {


ebx = ebx + 5;
var1 = var1 + 3
}

################################################
################################################

You might also like