COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
LAB MANUAL 8
Lab Title:
• String Instructions (MOVSB, MOVSW)
• Push and POP in stack
• Write data into data segment shown in memory
• Move data from data segment to internal registers
DEPARTMENT OF COMPUTING
© Copy Rights, Department of Computer Science, STMU Islamabad
All Rights Reserved
Prepared By: Mr. Shahid Raza
Revised By. Engr. Muhammad Haris Farooq
DEPARTMENT OF COMPUTING
2
List of Experiments
• String Instructions (MOVSB, MOVSW)
• Push and POP in stack
• Write data into data segment shown in memory
• Move data from data segment to internal registers
136
Assembly Language Programs 8086
The assembly language programming 8086 has some
rules such as:
• The assembly level programming 8086 code must be written in upper case letters
• The labels must be followed by a colon, for example:
label:
• All labels and symbols must begin with a letter
• All comments are typed in lower case
• The last line of the program must be ended with the END directive
• 8086 processors have two other instructions to access the data, such as WORD PTR
– for word (two bytes), BYTE PTR – for byte.
Figure 1.Op-Code and Operand
Op-code:
A single instruction is called as an op-code that can be executed by the CPU. Here the
‘MOV’ instruction is called as an op-code.
Operands:
A single piece data are called operands that can be operated by the op-code.
Example, subtraction operation is performed by the operands that are subtracted by
the operand.
Syntax:
SUB b, c
137
Assembly language instructions
A total of 116 instructions are available for the Intel 8086 microprocessor. All these
instructions with related examples are provided in this link. We will discuss a few
instructions necessary for understanding the later parts.
String Instructions
In 8086 assembly language, string instructions are a set of instructions designed to work with
strings of data in memory. They are particularly useful for tasks such as copying, comparing,
and searching within strings. Here's a description of two commonly used string instructions:
MOVS (Move String) and CMPS (Compare String). MOVSB and MOVSW are instructions
used for moving a byte (MOVSB) or a word (MOVSW) from the source address to the
destination address. These instructions are commonly used in string manipulation operations.
MOVS (Move String)
Copies a byte, word, or double word from the source address to the destination address,
then increments both the source and destination pointers.
Format:
MOVS destination, source
Example:
MOV SI, OFFSET Source ; Set SI to point to the source string
MOV DI, OFFSET Destination ; Set DI to point to the destination string
MOVSB ; Copies 1 byte from memory location pointed by SI (in DS) to
memory location pointed by DI (in ES). increment SI and DI
MOVSB ; Copy the next byte, incrementing SI and DI again
MOVSB ; Repeat for each byte until the null terminator is encountered
MOV CX, 5
REP MOVSB ; Copies 5 bytes from Source String to Dest String.
MOVSB (Move Byte):
Moves a byte of data from the address pointed to by the source index (SI) to the address
pointed to by the destination index (DI). After the move, both SI and DI are incremented
or decremented depending on the state of the direction flag (DF).
138
Format:
MOVSB
Example:
DATA SEGMENT
SRC_STRING DB 'ABC' ; Source string
DST_STRING DB 3 DUP (?) ; Destination string
buffer
DATA ENDS
CODE SEGMENT
START:
MOV AX, @DATA ; Load the data segment address into AX
MOV DS, AX ; Set the data segment register to the data segment address
MOV SI, OFFSET SRC_STRING ; Set SI to point to the source string
MOV DI, OFFSET DST_STRING ; Set DI to point to the destination string
MOV CX, 3 ; Set the loop counter to copy 3 bytes
REP MOVSB ; Repeat the move operation until CX becomes zero
; Terminate the destination string with '$' for printing
MOV BYTE PTR [DI], '$'
; Print the destination string
MOV AH, 09h ; DOS function to print string
MOV DX, OFFSET DST_STRING ; Load DX with the offset of the
destination string
INT 21h ; Call DOS to print the string
MOV AH, 4CH ; DOS function to exit
INT 21H ; Call DOS
CODE ENDS
END START
139
MOVSW (Move Word)
Moves a word (two bytes) from the memory location addressed by the source index (SI) to
the memory location addressed by the destination index (DI). After the move, both SI and
DI are automatically incremented or decremented depending on the state of the direction
flag (DF).
Format:
MOVSW
Example:
DATA SEGMENT
SRC_ARRAY DW 1234H, 5678H ; Source array of words
DST_ARRAY DW 2 DUP (?) ; Destination array buffer
DATA ENDS
CODE SEGMENT
START:
MOV AX, @DATA ; Load the data segment address into AX
MOV DS, AX ; Set the data segment register to the data segment address
MOV SI, OFFSET SRC_ARRAY ; Set SI to point to the source array
MOV DI, OFFSET DST_ARRAY ; Set DI to point to the destination
array
MOV CX, 2 ; Set the loop counter to copy 2 words
REP MOVSW ; Repeat the move operation until CX becomes zero
; Print the destination array
MOV AH, 02h ; BIOS function to print character
MOV DX, OFFSET DST_ARRAY ; Load DX with the offset of the destination array
ADD DX, 2 ; Skip the first byte as it contains the ASCII character of '1'
MOV CX, 4 ; Set CX to print 4 characters (2 words)
CALL PRINT_HEX_ARRAY ; Call procedure to print the array in hexadecimal format
MOV AH, 4CH ; DOS function to exit
INT 21H ; Call DOS
PRINT_HEX_ARRAY PROC
PUSH SI ; Preserve SI
MOV SI, DX ; Move the destination array offset to SI
140
PRINT_LOOP:
MOV DL, [SI] ; Load byte from memory into
DL MOV AH, 02h ; Set AH to print
character
INT 21h ; Print character in DL
INC SI ; Move to the next byte
LOOP PRINT_LOOP ; Repeat until CX becomes zero
POP SI ; Restore SI
RET ; Return from procedure
PRINT_HEX_ARRAY ENDP
CODE ENDS
END START
Result:
After execution, DST_ARRAY will contain 1234H, 5678H, printed as "34125678".
PUSH and POP Instructions
PUSH and POP are fundamental instructions used to manipulate the stack. Here's a detailed
explanation of these instructions.
PUSH Instruction:
The PUSH instruction places a word (16-bit value) onto the stack. It decrements the stack
pointer (SP) by 2 and then stores the value at the address pointed to by SP.
Format:
PUSH source
Source can be a register or a memory location. Commonly used registers include AX, BX,
CX, DX, SP, BP, SI, DI.
Example:
PUSH AX ; Push the value of AX onto the stack
POP Instruction:
The POP instruction removes a word (16-bit value) from the stack. It loads the word at the
address pointed to by SP into the destination operand and then increments SP by 2.
141
Format:
POP destination
Destination can be a register or a memory location. Commonly used registers include AX,
BX, CX, DX, SP, BP, SI, DI.
Example:
POP AX ; Pop the top value from the stack into AX
PUSH and POP Combine Example:
.MODEL SMALL
.STACK 100h
.DATA
; Data segment (empty in this example)
.CODE
MAIN PROC
MOV AX, 1234h ; Load AX with some value
MOV BX, 5678h ; Load BX with another value
PUSH AX ; Save AX onto the
stack PUSH BX ; Save BX onto the
stack
; Assume some operations here that might change AX and
BX MOV AX, 0 ; Clear AX
MOV BX, 0 ; Clear BX
POP BX ; Restore the original value of BX from the stack
POP AX ; Restore the original value of AX from the
stack
; At this point, AX should be 1234h and BX should be 5678h
MOV AH, 4Ch ; Terminate the
program INT 21h
MAIN ENDP
142
END MAIN
Explanation:
MOV AX, 1234h loads the value 1234h into register AX.
MOV BX, 5678h loads the value 5678h into register BX.
PUSH AX saves the current value of AX (which is 1234h) onto the stack.
PUSH BX saves the current value of BX (which is 5678h) onto the stack.
MOV AX, 0 and MOV BX, 0 clear the values of AX and BX, simulating
some operations that modify these registers.
POP BX restores the value from the stack into BX (which was 5678h).
POP AX restores the value from the stack into AX (which was 1234h).
MOV AH, 4Ch prepares for program termination.
INT 21h interrupts to terminate the program.
By using PUSH and POP, we preserved the original values of the registers AX and BX
even though they were altered in the middle of the program. This is a common use case
for these stack operations in assembly language programming.
143
Lab Activities:
1. Write an assembly program to write first 5 even numbers at memory location,
DS = 0300H and DI= 0000H.
; Program to write the first 5 even numbers to memory location DS = 0300H and
DI = 0000H
.model small
.stack 100h
.code
main:
; Set data segment to 0300H
mov ax, 0300h
mov ds, ax
; Initialize DI to 0000H
mov di, 0000h
; Initialize the counter to 5 (number of even numbers to write)
mov cx, 5
; Initialize the even number to 2
mov ax, 2
write_even_numbers:
; Write the even number to memory at DS:DI
mov [di], ax
; Increment DI to point to the next memory location (assuming word size, so
add 2)
add di, 2
; Increment the even number by 2
add ax, 2
; Decrement the counter
loop write_even_numbers
; Terminate program
mov ah, 4Ch
int 21h
end main
144
2. Write an assembly program to write first 10 odd numbers at memory location,
DS = 0300H and DI= 0000H.
; Program to write the first 10 odd numbers to memory location DS = 0300H and
DI = 0000H
.model small
.stack 100h
.code
main:
; Set data segment to 0300H
mov ax, 0300h
mov ds, ax ; Initialize DI to 0000H
mov di, 0000h ; Initialize the counter to 10 (number of odd numbers to write)
mov cx, 10 ; Initialize the odd number to 1
mov ax, 1
write_odd_numbers:
; Write the odd number to memory at DS:DI
mov [di], ax
; Increment DI to point to the next memory location (assuming word size, so
add 2)
add di, 2
; Increment the odd number by 2
add ax, 2
; Decrement the counter
loop write_odd_numbers
; Terminate program
mov ah, 4Ch
int 21h
end main
3. Write an assembly language program to store the words 1245, 6789 in the
memory segment called data segment.
; Program to store the words 1245 and 6789 in the data segment
.model small
.stack 100h
145
.data
word1 dw 1245h
word2 dw 6789h
.code
main:
; Initialize data segment
mov ax, @data
mov ds, ax
; Program logic is not required here as the data is already stored in the data
segment
; Terminate program
mov ah, 4Ch
int 21h
end main
4. Write an assembly language program to move the words (lab activity 3) into the
internal registers BX and CX.
; Program to move words from data segment into internal registers BX and CX
.model small
.stack 100h
.data
word1 dw 1245h
word2 dw 6789h
.code
main:
; Initialize data segment
mov ax, @data
mov ds, ax
; Move the word at the address of word1 into BX
mov bx, word1
; Move the word at the address of word2 into CX
mov cx, word2
; Terminate program
mov ah, 4Ch
146
int 21h
end main
5. Save the contents of two registers on the stack and then restore them.
; Program to save the contents of registers AX and BX on the stack and then
restore them
.model small
.stack 100h
.data
; No data required for this example
.code
main:
; Initialize data segment
mov ax, @data
mov ds, ax
; Set up initial values for AX and BX
mov ax, 1234h ; Example value for
AX mov bx, 5678h ; Example
value for BX
; Save AX and BX to the stack
push ax ; Save AX on the
stack push bx ; Save BX on the
stack
; Modify AX and BX to show that they are
saved mov ax, 9A9Ah ; New value for AX
mov bx, BCBCh ; New value for BX
; Restore AX and BX from the stack
pop bx ; Restore BX from the
stack pop ax ; Restore AX from the stack
; Terminate program
mov ah, 4Ch
int 21h
end main
147
6. Your Task is to execute the provided Assembly programs in EMU 8086, and
carefully document the output, noting any changes in registers, memory
operations, and any displayed results during execution.
1.
MOV AX, 0300H
MOV DS,AX
MOV DI,0000H
MOV AX,0000H
MOV CL, 05H
ABC:
MOV [DI], AX
INC DI
ADD AX, 02H
LOOP ABC
Hlt
2.
MOV AX,0300H
MOV DS,AX
MOV DI,0000H
MOV AX,0001H
MOV CL, 05H
ABC:
MOV [DI],AX
INC DI
ADD AX,02H
LOOP ABC
hlt
3.
MOV AX, 0300H
MOV DS, AX
MOV SI, 0000H
MOV DI, 0000H
148
MOV AX, 45H
MOV [DI],AX
INC DI
MOV AX, 12H
MOV [DI],AX
MOV CX,2000H
MOV CX, [SI]
4.
MOV AX, 0300H
MOV DS, AX
MOV SI, 0000H
MOV CX,2000H
MOV CX, [SI]
5.
MOV AX, 0200H
MOV DS, AX
MOV AX, 0300H
MOV ES, AX
6.
MOV DS:[0000],52H ; DATA STORING IN DATA
SEGMENT MOV DS:[0001],74H
MOV DS:[0002],8AH
7.
MOV AX, 0200H
MOV DS, AX
MOV AX, 0300H
MOV ES, AX
MOV DS:[0000],52H
MOV DS:[0001],74H
MOV DS:[0002],8AH
MOVSB
MOVSB
MOVSB
149
Lab Exercise and Summary:
Summary should cover Introduction, Procedure, Data Analysis and Evaluation.
150
Student’s Signature: Date:
151
Evaluation Criteria
Method of Evaluation: Viva, Practical Quiz
Excellent Good Satisfactory Unsatisfactory Poor Marks
Obtained
10 9-7 6-4 3-1 0
Assignment All tasks Most tasks Some tasks Most tasks All tasks were
completed were were were incomplete or
correctly in completed completed incomplete or incorrect.
given time correctly. correctly and incorrect and
and have a Tasks could have an have no Didn’t
complete be improved incomplete understanding perform tasks
understanding further and understanding and have no
have a understanding
complete
understanding
Total
References:
1. https://amritoo.hashnode.dev/a-beginners-guide-to-assembly-language-using-emu8086
2. https://github.com/amritoo/assembly-codes/blob/main/variable_and_io.asm
3. https://www.eng.auburn.edu/~sylee/ee2220/8086_instruction_set.html
4. https://www.eng.auburn.edu/~sylee/ee2220/8086_instruction_set.html#SBB
152