21st Caal Lab (Autosaved) PF
21st Caal Lab (Autosaved) PF
21st Caal Lab (Autosaved) PF
C++
Computer Science
STUDENT INFORMATION
Lab Session 7
Objective
To understand and familiarize with writing strings in Assembly language with 8086 emulator
environment
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.
String that is terminated by $ symbol is written in the Data segment under the
heading of .Data
Int 21h
;TITLE LAB 07
.MODEL SMALL
.STACK 100H
.DATA
MESSAGE1 DB "IQRA UNIVERSITY$"
.CODE
MAIN proc
MOV AX,@DATA ; Get address of DATA segment
;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
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:
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, 02H ; Move the single character output function into AH
register
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
E.g. 6826
- 2434
--------------------
43F2
--------------------
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
.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.
;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 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 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
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, 02H ; Move the single character output function into AH
register
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
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 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.
Exercise
Generate output for all the examples and attach in the lab
manual.
Eg.
Repeat Task 01 with subtraction
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\
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.
03
06
09
12
15
18
21
24
27
30
LAB SESSION 12
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
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,
CODE:
.model small
.stack 100h
.data
value dw 7
total db 9
less db 6
equals db 100
.code
main proc
mov cx,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