21st Caal Lab (Autosaved) PF

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

LABORATORY MANUAL

Computer Architecture and Assembly Language

C++

Computer Science
STUDENT INFORMATION

Name: FARRUKH ABBASI


___________________________________________

Registration No: 10424_________________________________

Semester: 3RD SEMESTER__


_________________________________

Program: Computer Architecture and Assembly Language


________________________________________
Email Address:
[email protected]______________________________
_

Lab Session 7

Objective
To understand and familiarize with writing strings in Assembly language with 8086 emulator
environment

Review of Assembly Language Directives:


PROC: Procedure The PROC directive marks the start of a named procedure
in the statement. Also, the types NEAR or FAR specify the type of the
procedure, i.e. whether it is to be called by the main program located within
64K of physical memory or not. For example, the statement RESULT PROC
NEAR marks the start of a routine RESULT, while is to be called by a program
located in the same segment of memory. The FAR directive is used for the
procedures to be called by the programs located in a different segment of
memory. The example statement is as follows:
Example:-
RESULT PROC NEAR
ROUTINE PROC FAR

ENDP:
END of Procedure In assembly language programming, the subroutines are
called procedures. The ENDP directive is used to indicate the end of a
procedure. A procedure is usually assigned a name, i.e. label. To mark the
end of a particular procedure, the name of the procedure, i.e. label may
appear as a prefix with the directive ENDP.
PROCEDURE START
:
: START ENDP
END: END of Program --The END directed marks the end of an assembly
language program. When the assembler comes across this END directive, it
ignores the source lines available later on.
OFFSET: It’s an assembly language directives. It can be used to indicate that you are
referencing the address of a value and not the value itself.

@data holds the address of data segment address

WRITING A STRING IN ASSEMBLY

What is String: String is a series of character that is written in “ ” and should


be ended upon a $ symbol that indicates an end of the string.

Where to write string:

String that is terminated by $ symbol is written in the Data segment under the
heading of .Data

What is function designated to Display string in


Assembly
Function 09H (Display string)
Instruction:
mov ah, 09h ; command to display string
int 21h ; invoke interrupt
How to retrieve the String in main Proc:
To retrieve string there are four steps:
Step 1 : Get the starting address of Data segment in to AX
register i.e.
mov ax, @data
Step 2: copy the data segment address in to Data
Segment register (DS)
mov ds, ax

Step 3: GET the starting address of string as Offset


string_name and copy it in any 16 bit register BX, DX
MOV DX, OFFSET MESSAGE1

Step 4: Call string display function by moving 09h in to AH


register followed by an interrupt call
MOV AH, 09H

Int 21h

;Example 1: Code to Display String

;TITLE LAB 07
.MODEL SMALL
.STACK 100H
.DATA
MESSAGE1 DB "IQRA UNIVERSITY$"
.CODE
MAIN proc
MOV AX,@DATA ; Get address of DATA segment

MOV DS, AX ; Move address of DATA segment into DS register

MOV DX, OFFSET MESSAGE1 ;Get offset address of MESSAGE1

MOV AH, 09H ;Move display string function into AH register

INT 21H ; Invoke DOS interrupt


MOV AH, 4CH
INT 21H
Main endp
END
;Example 2: Code to Display String
;In inform 0AH is for new line and 0DH is for carriage return

;TITLE LAB 07
.MODEL SMALL
.STACK 100H
.DATA
inform DB 0AH, 0DH, "I am a Coder 7898$"
.CODE
MAIN proc
MOV AX,@DATA ; Get address of DATA segment
MOV DS, AX ; Move address of DATA segment into DS register
MOV DX, OFFSET inform ;Get offset address of inform
MOV AH, 09H ;Move display string function into AH register
INT 21H ; Invoke DOS interrupt
MOV AH, 4CH
INT 21H
Main endp
END
Task:
Lab Session 8
Objective
To understand and familiarize with some more addition instructions with
8086 emulator environment
Review of Addition:
 ADD (Addition) Instruction
 The ADD instruction adds the value of destination to the
source and the result store in the destination register,
where source register remains unchanged.

 ADD Instruction format
 ADD Destination, Source
 ; Destination = Destination + Source

Example1 : ADD two decimal values and than two hexa


decimal values and observe the values of variables from
“vars” window.
.MODEL SMALL
.STACK 100H
.data
var1 db ‘1’
var2 db ‘4’
sum db 0
.code
main proc
mov ax, @data
mov ds, ax

mov dl, var1


mov bl, var2
add bl,dl
mov sum, bl
mov ah,4ch
int 21h
main endp
END

Some more addition instructions:


 INC (Increment) Instruction
 The INC (Increment) instruction increases the content of
register by 1.
 Increment instruction format:

 INC DESTINATION; Destination = Destination + 1

 E.g. MOV AH, 0FD H ; AH = FD H
 INC AH ; AH = FE H
 INC AH ; AH = FF H
 INC AH ; AH = 00 H
 INC AH ; AH = 01 H
INT interrupt number
 DEC (Decrement) instruction
 The DEC (decrement) instruction decreases the content of
register by 1.

 Decrement instruction format:

 DEC DESTINATIION; Destination = Destination - 1

 E.g. MOV AH, 02H ; AH = 02 H
 DEC AH ; AH = 01 H
 DEC AH ; AH = 00 H
 DEC AH ; AH = FF H
 DEC AH ; AH = FE H




 ADC (Add with carry) Instruction
 ADC instruction adds the two numbers with carry. When
two number are added carry may generated this carry store
by making the carry flag 1, if no carry is generated then
carry flag becomes 0.

 ADC Instruction format
 ADC Destination, Source; Destination = Destination
+ Source + Carry

 E.g. 27 92
 + 32 83
 --------------------
 5A 15
 --------------------

 MOV AL, 92H ; AL = 92 H
 MOV BL, 83H ; BL = 83 H
 ADD AL, BL ; AL = 92 H + 83 H Result is 15 H
and C = 1
 MOV CL, AL ; Saving result of lower byte
 MOV AL, 27H ; AL = 27 H
 MOV BL, 32H ; BL = 32 H
 ADC AL, BL ; AL = 27 H + 32 H +C (C = 1)
Result is 5A and C = 0
 MOV CH, AL ; Saving result of higher byte

 The CX Register contains 5A15 (Result)










 Practice Task:
 Design a proper assembly program to add
2793+3383 and save the result in DX register.

 second27 mov ah, 27 first 93 mov al,
93
 33 83 mov bl, 83
 Add al,bl
 Mov dl,al
 6







 111
 0 011
 0 111
 0 10



 1
 1
 0
Lab Session 9
Objective
 To understand and familiarize with some more arithmetic
instructions with 8086 emulator environment
Revision
Input/Output instructions
The processor cannot access the peripheral devices (like keyboard or monitor) directly. Microprocessor
use BIOS routines or DOS routines to access peripherals. The BIOS (Basic Input/Output System) routines
are store in ROM and directly run the I/O ports where DOS routine use BIOS routine to perform
operation that’s why has less complex than BIOS routine.

The BIOS and DOS both use INT (Interrupt) instruction, the interrupt is actually done by
number which determine nature of interrupt. The format of interrupt instruction is:

INT interrupt number

e.g. INT 21H ; Invoke DOS routine

INT 16H ; Invoke BIOS routine


These are the function number involved with AH register followed by INT
21h instruction as
MOV AH, 01H
INT 21H

01H Take input with echo


02H Display character

When INT 21H execute the DOS first see the function number in AH register, so all the function number
must be placed in AH register to execute.
Function 01H (Single character input with echo)
After instruction executes, the AL register get the ASCII Code of the
character and if non-character key (control key) is press then than AL
register get 00H. The 01H function also displays the character on screen.

MOV AH, 01H ; Move input key function to AH register

INT 21H ; Invoke DOS interrupt

Function 02H (Single character output)


This function displays the signal character whose ASCII Code value is
present in DL register.

MOV DL,’A’ ; Move the ASCII code of A ( A = 41H) in DL


register

MOV AH, 02H ; Move the single character output function into AH
register

INT 21H ; Invoke DOS interrupt

NEG instruction
The NEG instruction negates a value by finding 2's
complement of its single operand.
 This simply means multiply operand by -1.
 When a positive value is negated the result is negative.
 A negative value will become positive.
 Zero remains zero.
 For example,
mov al, -1 ; Load register 0000 0001
1’S COMPLEMENT 1111 1110 ADD 1
(2’S COMPLEMENT 1111 1111)= FF store in AL
neg al ; AL now has 1= 0000 0001

Example 1:
.model SMALL
.stack 100h
.data
.CODE
MOV AL,-2
NEG AL
MOV AH,04CH
INT 21H
Main endp
END
SUB (Subtract) Instruction
The SUB instruction subtracts the value of source from the value of destination, where
source register remains unchanged.
SUB Instruction format
SUB Destination, Source ; Destination = Destination - Source

SBB (Subtract with borrow) Instruction


SBB instruction subtracts the two numbers with carry. When two number are subtracted a
borrow may needed when borrow is taken carry flag is set to 1. The SBB instruction execute
it subtract two number with carry + flag value.

SBB Instruction format


SBB Destination, Source ; Destination = Destination - Source - Carry

E.g. 6826
- 2434
--------------------
43F2
--------------------

MOV AL, 26H ; AL = 26 H


MOV BL, 34H ; BL = 34 H
SUB AL, BL ; AL = 26 H - 34 H Result is F2 H and C = 1
MOV CL, AL ; Saving result of lower byte
MOV AL, 68H ; AL = 68 H
MOV BL, 24H ; BL = 24 H
SBB AL, BL ; AL = 68 H - 24 H - C (C = 1) Result is 43 and C = 0
MOV CH, AL ; Saving result of higher byte

The CX Register contains 43F2 (Result)


AAA (ASCII Adjust for Addition) Instruction
The AAA instruction converts the result in hexadecimal format to decimal format present in
AL register after adding two unpacked BCD or two ASCII digits. If lower nibble of AL
register is greater then (9) H or AF is set to 1 then AL is increment by (6) H and AH is
increment by (1) H. This instruction always clear upper nibble of AL register.
AAA instruction format
AAA ; ASCII adjust for addition
E.g. Consider (9)D+ (4)D = (13)D, but processor add it in hexadecimal format so its answer will
be (D)H, to convert hexadecimal format into decimal format AAA instruction add 6 is in (D) H
which makes (13) H so (03) H is store in AL register and (01) H is added in AH register.

MOV AL, 09D ; AL = 09H and AH = 00H


ADD AL, 04D ; AL = 0DH and AH = 00H
AAA ; AH = 01H and AL = 03H
AAS (ASCII Adjust for Subtraction) Instruction
The AAS instruction converts the result in hexadecimal format to decimal format present in
AL register after subtraction of two unpacked BCD. If lower nibble of AL register is greater
then (9)H or AF is set to 1 then AL is decrement by (6) H and AH is decrement by 1. This
instruction always clear upper nibble of AL register.
AAS instruction format
AAS ; ASCII adjust for addition

E.g. Consider (5)D - (7)D = - (8)D, but processor add it in hexadecimal format so its answer will
be (FE)H, to convert hexadecimal format into decimal format AAS instruction subtract 6 from
(FE) H which makes (F8) H so (08) H is store in AL register and (01) H is subtracted from AH
register. Note negative sign will not store so by checking AH register negative answer can
determine.
MOV AL, 05H ; AL = 09H and AH = 00H
SUB AL, 07H ; AL = FEH and AH = 00H
AAS ; AL = 08H and AH = FFH (00 H – 01H = FFH)
Example 2: Design an assembly program for adding in
one digit decimal number.
ADD 9D
+ 9D
=18D

;small 1 digit example EXAMPLE_PROG_AAA

.model SMALL
.stack 100h
.data
.CODE
Main PROC
MOV AL, 09D ; AL = 09H and AH = 00H
ADD AL, 09D ; AL = 0DH and AH = 00H
AAA

MOV AH,04CH
INT 21H
Main endp
END
EXAMPLE: 3 Design an assembly program for adding
in one digit decimal number AND print output on console
screen.

;small 1 digit decimal number print output on


console screen

;EXAMPLE_PROG_AAA
.model SMALL ; initialize memory
.stack 100h ; initialize stack segment
.data ;initialize data segment
.CODE ;initialize code segment
MAIN PROC
; we have to do ADD 8 + 7=15
MOV AL, 08D ; AL = 08H and AH = 00H
ADD AL, 07D ; AL = 07DH and AH =00H
AAA

MOV BL,AL ; saving contents of al into bl


Mov dl,Ah ; mov value of ah into DL for print output
ADD DL,30H ;to convert ASCII character into digit
Mov ah,02; for character output
Int 21h
Mov dl,Bl ; now moving bl into dl to print it
ADD DL,30H ;to convert ASCII character into digit
Mov ah,02; for character output
Int 21h
MOV AH,04CH
INT 21H
Main endp
END
Example 4: Design an assembly program, to
add two digit decimal number
;EXAMPLE_PROG_AAA
.model SMALL
.stack 100h
.data
.CODE
Main PROC
; ADD 29
; + 39
; =68
MOV AL, 09D ; AL = 09H and AH = 00H
ADD AL, 09D ; AL = 0DH and AH = 00H
AAA
mov bx,ax

MOV AL, 02D ; AL = 09H and AH = 00H


ADD AL, 03D ; AL = 0DH and AH = 00H
AAA
add bh, al

MOV AH,04CH
INT 21H
Main endp
END
Example 5: Design an assembly program, to
subtract one digit decimal number
;EXAMPLE_PROG_AAs
.model SMALL
.stack 100h
.data
.CODE
Main PROC
; subtract 7
; - 6
; =1
MOV AL, 07D ; AL = 07H and AH = 00H
sub AL, 06D ; AL = 06H and AH = 00H
AAs
mov bl,al

MOV AH,04CH
INT 21H
Main endp
END
Example 6: Design an assembly program, to
subtract two digit decimal number
;EXAMPLE_PROG_AAs
.model SMALL
.stack 100h
.data
.CODE
Main PROC
; subtract 52
; - 13
; =39
MOV AL, 02D ; AL = 02H and AH = 00H
sub AL, 03D ; AL = 03H and AH = 00H
AAs
mov bl,al

MOV AL, 05D ; AL = 05H and AH = 00H


sbb AL, 01D ; AL = 01H and AH = 00H
AAs
mov bh, al

MOV AH,04CH
INT 21H
Main endp
END
Tasks:
1) Generate output for all the examples and attach in the lab manual.

2) Write a program which asks user to enter two numbers of 1 digit each, adds it and
displays its output. Remembering for entering a number you have to sub al,30H to
convert ASCII character into number where as ENTER DIGIT is printed string in
assembly language (lab 7)
Eg.
3) Repeat Task 02 with subtraction
Lab Session 10
Objective
 To understand and familiarize with some more arithmetic
instructions with 8086 emulator environment
 Revision

 Input/Output instructions
The processor cannot access the peripheral devices (like keyboard or monitor) directly. Microprocessor
use BIOS routines or DOS routines to access peripherals. The BIOS (Basic Input/Output System) routines
are store in ROM and directly run the I/O ports where DOS routine use BIOS routine to perform
operation that’s why has less complex than BIOS routine.
The BIOS and DOS both use INT (Interrupt) instruction, the interrupt is actually done by
number which determine nature of interrupt. The format of interrupt instruction is:
INT interrupt number
e.g. INT 21H ; Invoke DOS routine
INT 16H ; Invoke BIOS routine

 These are the function number involved with AH register followed by


INT 21h instruction as
 MOV AH, 01H
 INT 21H

01H Take input with echo


02H Display character


When INT 21H execute the DOS first see the function number in AH register, so all the function number
must be placed in AH register to execute.


Function 01H (Single character input with echo)
After instruction executes, the AL register get the ASCII Code of the
character and if non-character key (control key) is press then than AL
register get 00H. The 01H function also displays the character on screen.

MOV AH, 01H ; Move input key function to AH register

INT 21H ; Invoke DOS interrupt

Function 02H (Single character output)


This function displays the signal character whose ASCII Code value is
present in DL register.
MOV DL,’A’ ; Move the ASCII code of A ( A = 41H) in DL
register

MOV AH, 02H ; Move the single character output function into AH
register

INT 21H ; Invoke DOS interrupt


 SBB (Subtract with borrow) Instruction
 SBB instruction subtracts the two numbers with carry. When two number are subtracted a
borrow may needed when borrow is taken carry flag is set to 1. The SBB instruction
execute it subtract two number with carry + flag value.

 SBB Instruction format
 SBB Destination, Source ; Destination = Destination - Source - Carry

 7
 E.g. 68 126
 - 24 34
 --------------------
 43 F2
 --------------------

 MOV AL, 26H ; AL = 26 H
 MOV BL, 34H ; BL = 34 H
 SUB AL, BL ; AL = 26 H - 34 H Result is F2 H and C = 1

 MOV CL, AL ; Saving result of lower byte

 MOV AL, 68H ; AL = 68 H
 MOV BL, 24H ; BL = 24 H
 SBB AL, BL ; AL = 68 H - 24 H - C (C = 1) Result is 43 and C = 0
 MOV CH, AL ; Saving result of higher byte

 The CX Register contains 43F2 (Result)





 AAA (ASCII Adjust for Addition)
Instruction
 The AAA instruction converts the result in hexadecimal format to decimal format present

in AL register after adding two unpacked BCD or two ASCII digits.



 If lower nibble of AL register is greater then (9) H or AF is set to 1 then AL is increment
by (6)H and AH is increment by (1) H. This instruction always clear upper nibble of AL
register.
 AAA instruction format
 AAA ; ASCII adjust for addition
 E.g. Consider (9)D+ (4)D = (13)D,
 but processor add it in hexadecimal format so its answer will be (D) H, to convert
hexadecimal format into decimal format AAA instruction add 6 is in (D) H which makes
(13) H so (03) H is store in AL register and (01) H is added in AH register.

 MOV AL, 09D ; AL = 09H and AH = 00H
 ADD AL, 04D ; AL = 0DH and AH = 00H
 AAA ; AH = 01H and AL = 03H

 AAS (ASCII Adjust for Subtraction)


Instruction
 The AAS instruction converts the result in hexadecimal format to decimal format present
in AL register after subtraction of two unpacked BCD. If lower nibble of AL register is
greater then (9)H or AF is set to 1 then AL is decrement by (6) H and AH is decrement by
1. This instruction always clear upper nibble of AL register.
 AAS instruction format
 AAS ; ASCII adjust for addition

 E.g. Consider (5)D - (7)D = - (8)D, but processor add it in hexadecimal format so its answer
will be (FE)H, to convert hexadecimal format into decimal format AAS instruction
subtract 6 from (FE) H which makes (F8) H so (08) H is store in AL register and (01) H is
subtracted from AH register. Note negative sign will not store so by checking AH register
negative answer can determine.



















 MOV AL, 05H ; AL = 05H and AH = 00H
 SUB AL, 07H ; AL = FEH and AH = 00H
 AAS ; AL = 08H and AH = FFH (00 H – 01H = FFH)


Example 1: Design an assembly program, to
subtract one digit decimal number
;EXAMPLE_PROG_AAs
.model SMALL
.stack 100h
.data
.CODE
Main PROC
; subtract 7
; - 6
; =1
MOV AL, 07D ; AL = 07H and AH = 00H
sub AL, 06D ; AL = 06H and AH = 00H
AAs
mov bl,al

MOV AH,04CH
INT 21H
Main endp
END
Example 2: Design an assembly program, to
subtract two digit decimal number
;EXAMPLE_PROG_AAs
.model SMALL
.stack 100h
.data
.CODE
Main PROC
; subtract 52
; - 13
; =39
MOV AL, 02D ; AL = 02H and AH = 00H
sub AL, 03D ; AL = 03H and AH = 00H
AAs
mov bl,al

MOV AL, 05D ; AL = 05H and AH = 00H


sbb AL, 01D ; AL = 01H and AH = 00H
AAs
mov bh, al

MOV AH,04CH
INT 21H
Main endp
END
Example 3 Display

 Multiplication Instruction
 There are two instructions for multiplication, MUL
(Multiplication) and IMUL (Integer Multiplication) Instruction.

 Instruction format are:
 MUL Source ; Unsigned multiplication
 IMUL Source ; Integer multiplication for
Signed number
 The source may be a register or memory but not a constant.
Also the source value and the multiplying value should be
of same magnitude.

 Byte Form Multiplication


 In the byte form of multiplication one number should
present in source and other in AL register. The result may
be of 16-bit (1 word) (8+8 )which is stored in AX register.

 Word Form Multiplication


 In the word form of multiplication one number is present in
source and other is in AX register. The result may be of 32-
bit (Double word) (16+16 )so most significant 16-bit goes
in DX and least significant 16-bit goes to AX register.

 AAM (ASCII Adjust for Multiplication)


Instruction
 The AAM instruction converts the result of two BCD digit
multiplications into unpacked BCD format. This instruction
convert the result into BCD whose value is lower then 100 D
or 64H, this is because the result is store in AX register in
the unpacked form that are AL and AH so largest value is
99D which store as AH = 09 and AL = 09 . The operation
for the instruction is the result which is store in AL register
is divided by 10D and the quotient store in AH register and
while remainder store in AL register.

 E.g. MUL Instruction
 MOV AL, 05D ; Move value in AL which is
source
 MOV BL, 07D ; Move value in BL which is
source
 MUL BL ; AX = 05H x 07H = 23H
 AAM ; AX = 0305H (5 x 7 = 35)
 E.g. IMUL Instruction
 MOV AL,-01D ; AL = FFH
 MOV BL,-01D ; BL = FFH
 IMUL BL ; AX = FF H x FF H = 0001H


 Example 3 Displaying result of multiplying 8Dx 9D
 .model SMALL
 .stack 100h
 .data
 .CODE
 Main PROC
 ; subtract 7
 ; - 6
 ; =1

 mov al,08D
 mov bl,09D
 imul bl ; AL = 07H and AH = 00H

 aam
 mov cl,al
 mov dl, ah
 add dl,30h
 mov ah , 2
 int 21h

 mov dl, cl
 add dl,30h
 mov ah , 2
 int 21h

 MOV AH,04CH
 INT 21H
 Main endp
 END

 Multiplication Instruction table
 Fu  Operation
Instruction syntax  Registers  Registers
ncti  type used in used to
on  Operation store result
   Source may be register
The 16-bits (1 word)
 MUL
 Multiplication
Byte  byte or memory byte
result store in
Sourc  and second byte inAX register
e Format  AL register
 for  Source may  The result is
unsign be word of 32-bits
ed register or (double
numbe Word memory word). The
r Format word and most
 second word significant
 IMUL Source in AX 16-bits store
register in DX
register while
the least
significant
 for
16-bits store
signed
in AX
numbe
register
r

Exercise
 Generate output for all the examples and attach in the lab
manual.

 Write a program which asks user to enter two numbers of 1


digit each, adds it and displays its output. Remembering
for entering a number you have to sub al,30H to convert
ASCII character into number where as ENTER DIGIT is
printed string in assembly language (lab 7)

Eg.
 Repeat Task 01 with subtraction




4)Write a program to generate the following sequence


(1, 2, 4, 8, 16)
Lab Session 11
Call, and Loop Instructions
Objective
 Understand working of Procedure (CALL instruction) and Stack
 Implement LOOP instruction

Theory
 The call instruction jump to target (called subroutine or procedure) execute the lines
until reach to the RET instruction, then return back to the next line from where its called.

 CALL and RET instruction
 The Subroutines (procedures) in 8086/88 assembly language are design by using CALL
and RET instruction. The CALL instruction use to call the procedure and RET
instruction jump back the flow to the next line from where it is call.
















 When procedure is call the processor first store (PUSH) the address of next line on the
stack and jump to the procedure. The procedure execute until RET (return) instruction
execute; then processor get back (POP) the address from stack (the address of next line
which is store when procedure is call) and jump to the next line of the CALL instruction.
 LEA: Load Effective Address instruction that loads
offset address of a string to a register
 Syntax:
 LEA DX, String_name


 Example 1: In the following program a procedure of display string function is
made by the name of DISPLAY.
 .model SMALL
 .stack 100h
 .data
 message1 db 0AH, "Iqra uni$"
 message2 db 0AH, "closed$"
 message3 db 0AH, 0dh, "so sad$"
 .CODE
 Main PROC
 MOV AX, @DATA
 MOV DS, AX
 mov DX, offset MESSAGE1
 CALL DISPLAY
 LEA DX, MESSAGE2
 CALL DISPLAY
 LEA DX, MESSAGE3
 CALL DISPLAY
 JMP END_PROG
 DISPLAY: MOV AH, 09H ; Display function subroutine
 INT 21H
 RET
 END_PROG:

 The LOOP Instruction
 The LOOP instruction repeats the sequence of instructions. The LOOP instruction
jumps on target to the number of times the value present in CX register.
 The LOOP instruction execute in following sequence.
 Decrease CX register.
 Compare CX with 0.
 If CX = 0 then goto next line else jump on Target.

 LOOP Instruction format:
 LOOP Target ; The target should be written before the LOOP
instruction

 Example:
 MOV CX, 05H
 MOV AL, 00H
 MOV BL, 09H
 HERE: INC AL
 DEC BL
 LOOP HERE
 After completion of the program values present in register are Al = 05H, = BL = 04H
and CX = 00H






























 The LOOP instruction flow

Sample Program:
SOURCE CODE:
Object: Title a program that prints a character 100 times.
.model small
 tack 100h
.data
.code
Main proc
mov cx, 100 ;number of times loop will execute
print: ;loop starts from here\

mov ah, 02h ;display a character


mov dl, ‘*’ ;ASCII code of character 0

int 21h
loop print ;executes the FOR loop
Main
endp
End
Exercise
Task #01:Write a program to print ‘#’100 times using linefeed and carriage return.
Task#02:Write a program to print your name 10 times using linefeed and carriage return
 Write a program which ask user to enter a digit (from 1 to 9 only) and then print the table
of that number.

The program is look like this


Please enter a digit = 3
(User press 3 and then press Enter key)

03
06
09
12
15
18
21
24
27
30
LAB SESSION 12

Implementing Branching in x86 Assembly language


 THEORY
Computers derive much of their power from their ability to selectively execute code and from the
speed at which they execute repetitive algorithms. Programs in HLL such as Java or C++ use if-then, if-
then-else, and case structures to selectively execute code and loop structures such as while (pre-test)
loops, until (post-test) loops, and for (counter-controlled) loops to repetitively execute code. Some HLLs
have a goto for unconditional branching.
Somewhat more primitive languages like older versions of FORTRAN or BASIC, depend on
fairly simple if statements and abundance of goto statements for both selective execution and looping.
The 80x86 assembly language programmer's job is similar to the old FORTRAN or BASIC programmer's
job. The 80x86 processor can execute some instructions that are roughly comparable to for statements,
but most branching and looping is done with 80x86 statements that are similar to, but even more primitive
than, simple if and goto statements.
 Jump instruction
 Unlikely the CALL instruction jump instruction has same working except that the jump
instruction not return. The Jumps instruction can distinguish into two types:
 Unconditional Jump
 Conditional Jump

 1) Unconditional Jump
 The unconditional jump transfers the control of program without checking any
condition. The unconditional jump can go any where in the program and has no
limitation.
 Unconditional Jump Format
 JMP Target ; The target is the label
 2) Conditional Jump
 The conditional jump transfers the controls of program by first watching the
condition. The conditional jump depend on the value of status flag. If condition is true
then it jump on target else it goes on the next line. Also all unconditional jumps are
short jump means the target must present with in range of -128 byte to +127 byte.
















 The working of conditional jump

 Conditional jump can be categorized into three types:

 1) The Signed Jump
 JG / JNLE Target ; Jump if Greater / (ZF = 0 & SF =
OF)
 ; Jump if Not Less or Equal
 JGE / JNL Target ; Jump if Greater or Equal / (SF = OF)
 ; Jump if Not Less
 JL / JNGE Target ; Jump if Less / (SF < > OF)
 ; Jump if Not Greater or Equal
 JLE / JNG Target ; Jump if Less or Equal / (ZF = 0 & SF <
> OF)
 ; Jump if Not Greater

 2) The Unsigned Jump
 JA / JNBE Target ; Jump if Above / (CF = 0 & ZF
= 0)
 ; Jump if Not Below or Equal
 JAE / JNB Target ; Jump if Above or Equal / (CF = 0)
 ; Jump if Not Below
 JB / JNAE Target ; Jump if Below / (CF = 1)
 ; Jump if Not Above or Equal
 JBE / JNA Target ; Jump if Below or Equal / (CF = 1 or ZF
= 1)
 ; Jump if Not Above

 3) The Single Flag Jump
 JZ / JE Target ; Jump if Zero/ Jump if Equal (ZF = 1)
 JNZ / JNE Target ; Jump if Not Zero/ Jump if Not Equal (ZF = 0)
 JC Target ; Jump if Carry (CF = 1)
 JNC Target ; Jump if No Carry (CF = 0)
 JP / JPE Target ; Jump if Parity/ Jump if Parity Even (PF = 1)
 JNP /JPO Target ; Jump if No Parity/ Jump if Parity Odd (PF = 0)
 JO Target ; Jump if Overflow (OF = 1)
 JNO Target ; Jump if No Overflow (OF = 0)
 JS Target ; Jump if negativeSign (SF = 1)
 JNS Target ; Jump if No negativeSign (SF = 0)


 The difference between Signed jump and Unsigned jump is only status of Sign Flag
(SF). The Signed jump watch the status of Signed flag with other flag whereas the
Unsigned jump does not watch signed flag. The Single Flag jump only watch the
status of the desire flag only.

 The Signed Jump example:
 CMP designation, source
 JG Target ; Jump if greater
 The Unsigned Jump example:
 CMP designation, source
 JA Target ; Jump if above

 The above instructions having same working that is jump if destination is greater than
source but the Signed jump see SF(signed flag), CF(carry flag) and ZF(zero flag)
where as Unsigned jump see only CF(carry flag) and ZF(zero flag).

 The Single flag jump
 CMP designation, source
 JZ Target ; Jump if designation = source
 and
 JC Target ; Jump if carry
 JP Target ; Jump if parity

 Example:
 1) The following program takes decimal number from keyboard and if user
presses button other then any decimal number then it take again.


 .MODEL SMALL
 .STACK 100H
 .data
 message1 db 0AH, 0dh, "Enter decimal number$"
 .CODE
 Main PROC

 AGAIN:
 Mov AX, @data
 Mov ds, ax
 Mov dx, offset message1
 Mov AH,09H
 Int 21H
 MOV AH, 01H ; Take input function
 INT 21H
 CMP AL, ‘0’
 JB AGAIN ; Jump if below than 0
 CMP AL, ‘9’
 JA AGAIN ; Jump if greater than 9
 mov ah,4ch
 int 21h
 main endp
 END
 The CMP Instruction
 In the above examples the unconditional jump depend on the values present in the
Status Flag, but how the flag is affect to meet condition. This is done by CMP
instruction, the CMP instruction compare the destination with source by subtracting
source value from destination value, the result is not store in operand but according to
result the bits of status flag register affected. The CMP Instruction is used to compare
Byte or Word, to check status of Bit(s) see TEST instruction. (Note that the CMP is
instruction is same as SUB instruction except the answer is not store in the
designation in CMP instruction).

 The TEST Instruction
 The TEST instruction perform logical AND operation to affect the flags but result
does not store. The TEST instruction is used to check specific bit(s) are set or clear.
 Example:
 The only difference between upper case character ASCII and lower case
character ASCII is of bit 6 which is ‘1’ in lower case character ASCII and ‘0’ in
upper case character ASCII. The following program check the ASCII present in AL
register, if it is upper case the program display ‘U’ character on screen and if it is
lower case then display ‘L’.



 Mov AL, 00100000B
 TEST AL,00100000B
 JE LOWER
 MOV DL,’U’
 MOV AH,02H
 INT 21H
 JMP END_PROG
 LOWER:
 MOV DL,’L’
 MOV AH,02H
 INT 21H
 END_PROG:

TASK

 1) The following program takes only + sign and – sign from keyboard and jump on
that function, and if user presses any other button then it take again.

.MODEL SMALL
.STACK 100H
.data
message1 db 0AH, 0dh, "Enter character$"
.CODE
Main PROC

AGAIN:
Mov AX, @data
Mov ds, ax
Mov dx, offset message1
Mov AH,09H
Int 21H
MOV AH, 01H ; Take input function
INT 21H

CMP AL, '+' ; Compare with + sign

JZ ADDITION ; If + sign enter then jump to ADDITION


CMP AL, '-' ; Compare with - sign
JE SUBTRACTION ; If - sign enter then jump to SUBTRACTION
JMP AGAIN ; Jump to again if all condition were false

ADDITION:
Mov ch,04H
Mov bl, 06H
mov ax,00000000B
Add bl,ch
mov al,bl

aaa
mov bx,ax
mov dl,bh
add dl,30H
mov ah, 02H
int 21h
mov dl,bl
add dl,30H
mov ah, 02H
int 21h
JMP END_PROG
SUBTRACTION:
Mov ch,06H
Mov bl, 05H
mov ax,00000000B
sub bl,ch
mov al,bl

aas
mov bx,ax
mov dl,bh
add dl,30H
mov ah, 02H
int 21h
mov dl,bl
add dl,30H
mov ah, 02H
int 21h

JMP END_PROG
END_PROG:

mov ah,4ch
int 21h
main endp
END
2- Change this pseudo code into assembly language: Assuming total and value
are variables stored in data segment of memory and count is in CX,

if (total <=100) or (count = 10)


then
add value to total;
end if;

CODE:
.model small
.stack 100h
.data
value dw 7
total db 9
less db 6
equals db 100
.code

main proc
mov cx,100

cmp total,100 ; compare it to 100

jnl if_less; we have applied jump if not less to 10 so jump on label large

if_less:
inc less

cmp bx,10

jl if_equals

if_equals:
inc equals

main endp
end
3-Assuming that the value is variable that is moved in BX and smallCount and
largeCount are variables in memory, generate the corresponding assembly
language c
ode


if value < 10
then
add 1 to smallCount;
else
add 1 to largeCount;
end if;
CODE:
.model small
.stack 100h
.data
value dw 3
large_largeCount db 5
smallCount db 4
.code

main proc

mov bx,value
cmp bx,10 ; compare it to 10

jnl large; we have applied jump if not less to 10 so jump on label large

large:
inc large_largeCount

cmp bx,10
jl small; if less so jump on label small

small:
inc smallCount

main endp
end

You might also like