LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
Lab No. 02
REGISTERS, MEMORY SEGMENTAION AND BUFFERS IN
80X86 EMULATOR ASSEMBLY LANGUAGE
Objective:
To understand and analyze the operation of registers, types and segmentation in 80X86 and
the concept of buffer, reading and writing the data in buffer.
Theory:
1) Registers:
Registers are a type of computer memory used to quickly accept, store, and transfer data
and instructions that are being used immediately by the CPU. A Processor register may
hold an instruction, a storage address, or any data such as bit sequence or individual
characters. Following are the main types of registers.
A. Segment Registers:
Segmentation is the process in which the main memory of the computer is logically
divided into different segments and each segment has its own base address. It is
basically used to enhance the speed of execution of the computer system, following are
the basic segment registers.
o STACK SEGMENT REGISTER (SS):
It is used for addressing stack segment of the memory. The stack segment is that
segment of memory which is used to store stack data.
o DATA SEGMENT REGISTER (DS):
It points to the data segment of the memory where the data is stored.
o CODE SEGMENT REGISTER (CS):
It is used for addressing memory location in the code segment of the memory,
where the executable program is stored.
o EXTRA SEGMENT REGISTER (ES):
It also refers to a segment in the memory which is another data segment in the
memory.
B. General Registers:
General purpose registers are used to store temporary data within the microprocessor.
There are 8 general purpose registers in 8086 microprocessor. Following are the main
types of general purpose registers.
o AX Register:
This is the accumulator. It is of 16 bits and is divided into two 8-bit registers AH
and AL to also perform 8-bit instructions. It is generally used for arithmetical and
logical instructions but in 8086 microprocessor it is not mandatory to have
accumulator as the destination operand.
SECTION: A 1
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
o BX Register:
This is the base register. It is of 16 bits and is divided into two 8-bit registers BH
and BL to also perform 8-bit instructions. It is used to store the value of the offset.
o CX Register:
This is the counter register. It is of 16 bits and is divided into two 8-bit registers CH
and CL to also perform 8-bit instructions. It is used in looping and rotation.
o DX Register:
This is the data register. It is of 16 bits and is divided into two 8-bit registers DH
and DL to also perform 8-bit instructions. It is used in multiplication an input/output
port addressing.
o SP Register:
This is the stack pointer. It is of 16 bits. It points to the topmost item of the stack.
If the stack is empty the stack pointer will be (FFFE)H. It’s offset address relative
to stack segment.
o BP Register:
This is the base pointer. It is of 16 bits. It is primary used in accessing parameters
passed by the stack. It’s offset address relative to stack segment.
o SI Register:
This is the source index register. It is of 16 bits. It is used in the pointer addressing
of data and as a source in some string related operations. It’s offset is relative to
data segment.
o DI Register:
This is the destination index register. It is of 16 bits. It is used in the pointer
addressing of data and as a destination in some string related operations. It’s offset
is relative to extra segment.
C. Flag Register:
The Flag register is a Special Purpose Register. Depending upon the value of result
after any arithmetic and logical operation the flag bits become set (1) or reset (0).
Following are the main 9 flag registers.
o Sign Flag (S):
After any operation, if the MSB (B(7)) of the result is 1, it indicates the number is
negative and the sign flag becomes set, i.e. 1. If the MSB is 0, it indicates the
number is positive and the sign flag becomes reset i.e. 0.
o Zero Flag (Z):
After any arithmetical or logical operation if the result is 0 (00)H, the zero flag
becomes set i.e. 1, otherwise it becomes reset i.e. 0.
o Auxiliary Carry Flag (AC):
This flag is used in BCD number system(0-9). If after any arithmetic or logical
operation D(3) generates any carry and passes on to B(4) this flag becomes set i.e.
1, otherwise it becomes reset i.e. 0.
SECTION: A 2
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
o Parity Flag (P) – If after any arithmetic or logical operation the result has even
parity, an even number of 1 bits, the parity register becomes set i.e. 1, otherwise it
becomes reset i.e. 0.
o Carry Flag (CY):
Carry is generated when performing n bit operations and the result is more than n
bits, then this flag becomes set i.e. 1, otherwise it becomes reset i.e. 0.
o Overflow Flag (O) :
This flag will be set (1) if the result of a signed operation is too large to fit in the
number of bits available to represent it, otherwise reset (0).
o Directional Flag (D):
This flag is specifically used in string instructions. If directional flag is set (1),
then access the string data from higher memory location towards lower memory
location. If directional flag is reset (0), then access the string data from lower
memory location towards higher memory location.
o Interrupt Flag (I) :
This flag is for interrupts. If interrupt flag is set (1), the microprocessor will
recognize interrupt requests from the peripherals. If interrupt flag is reset (0), the
microprocessor will not recognize any interrupt requests and will ignore them.
o Trap Flag (T):
This flag is used for on-chip debugging. If trap flag is set (1), the CPU
automatically generates an internal interrupt after each instruction, allowing a
program to be inspected as it executes instruction by instruction. If trap flag is
reset (0), no function is performed.
Procedure:
In order to take an input of string of characters from user into 80X86 a function called 0AH
and Buffer string is used.
Following are the steps used to take the input of characters in 80X86 microprocessor.
1. Create a buffer string and declare it in .DATA section of emulator, start with giving
the string a name, then after a comma allocates the number of bytes for the actual length
of buffer total bytes used for the input, then allocate the number of bytes for the buffer
length anticipated and then add DUP (?) at the end.
2. Transfer 0AH function to the AH resistor by using the MOVE command.
3. Then transfer the buffer string into the DX resistor by using the LEA command.
4. In the end call the INT 21H function and return the controls to DOS.
SECTION: A 3
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
LAB TASK (1)
Objective: Compile and run the following source code and show the contents of the buffer
input taken by the user.
Program:
Source code:
org 100h
.MODEL SMALL
.STACK 200
.DATA
PROMPT DB 'Enter your name(max. 9 characters): ', 0DH, 0AH, '$'
BUFFER DB 10, 11 DUP (?) ; Allocate 13 bytes for BUFFER
; and put value 10 in the 1st byte.
.CODE
.STARTUP ; this directive initializes the DS and CS segments.
LEA DX, PROMPT ; display prompt
MOV AH, 09H
INT 21H
MOV AH, 0AH ; read into buffer
LEA DX, BUFFER
INT 21H
MOV AH, 4CH
INT 21H
END
ret
Output Result:
Figure 1: Output of program
o In this task, we learned how to use BUFFER, how to take string input and show it on screen.
SECTION: A 4
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
LAB TASK (2)
Objective: Modify the code below for the 12 character input
Program:
Source code:
org 100h
.MODEL SMALL
.STACK 100H
.DATA
CRLF DB 0DH, 0AH, '$'
PROMPT DB 'Enter your name (max. 9 characters):' ,0DH, 0AH, '$'
STRING1 DB 'Mr./Miss ','$'
STRING2 DB ' has ambitions to do something great for his/her country.','$'
BUFFER DB 10, 12 DUP (?)
.CODE
.STARTUP
LEA DX, PROMPT ; display prompt
MOV AH, 09H
INT 21H
MOV AH, 0AH ; read into buffer
LEA DX, BUFFER
INT 21H
LEA DX, CRLF ; move cursor to next line
MOV AH, 09H
INT 21H
LEA DX, STRING1 ; display string1
MOV AH, 09H
INT 21H
MOV AH, 09H
MOV BH, 00H
MOV BL, BUFFER [1] ; BX gets actual buffer length
MOV BUFFER [BX+2],'$' ; put a $ sign at the end of buffer
LEA DX, BUFFER [2] ; load actual start of string
INT 21H
LEA DX, STRING2 ; display string2
MOV AH, 09H
INT 21H
LEA DX, CRLF ; move cursor to next line
MOV AH, 09H
INT 21H
MOV AH, 02H ; display number of characters read if less than 10
MOV DL, BUFFER [1] ; read second byte of buffer
ADD DL, 30H ; convert to number
INT 21H
MOV AH, 4CH
INT 21H
END
ret
SECTION: A 5
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
Modified Source code:
org 100h
.MODEL SMALL
.STACK 100H
.DATA
CRLF DB 0DH, 0AH, '$'
PROMPT DB 'Enter your name (max. 9 characters):' ,0DH, 0AH, '$'
STRING1 DB 'Mr./Miss ','$'
STRING2 DB ' has ambitions to do something great for his/her country.','$'
BUFFER DB 13, 15 DUP (?)
.CODE
.STARTUP
LEA DX, PROMPT ; display prompt
MOV AH, 09H
INT 21H
MOV AH, 0AH ; read into buffer
LEA DX, BUFFER
INT 21H
LEA DX, CRLF ; move cursor to next line
MOV AH, 09H
INT 21H
LEA DX, STRING1 ; display string1
MOV AH, 09H
INT 21H
MOV AH, 09H
MOV BH, 00H
MOV BL, BUFFER [1] ; BX gets actual buffer length
MOV BUFFER [BX+2],'$' ; put a $ sign at the end of buffer
LEA DX, BUFFER [2] ; load actual start of string
INT 21H
LEA DX, STRING2 ; display string2
MOV AH, 09H
INT 21H
LEA DX, CRLF ; move cursor to next line
MOV AH, 09H
INT 21H
MOV AH, 02H ; display number of characters read if less than 10
MOV DL, BUFFER [1] ; read second byte of buffer
ADD DL, 30H ; convert to number
INT 21H
MOV AH, 4CH
INT 21H
END
ret
SECTION: A 6
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
Output Result:
Figure 2 & 3: Output of program
o In this task we learned, how to modify a BUFFER according to our required needs.
SECTION: A 7
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
LAB ASSIGNMENT
Objective#1: Prompt separately for first and second name of the user, both must be placed
inside a buffer separated by a space, also display that buffer to the user.
Program:
Source code:
org 100h
.MODEL SMALL
.STACK 100H
.DATA
CRLF DB 0DH, 0AH, '$'
m1 DB 'Enter your first name (max. 9 characters):' ,0DH, 0AH, '$'
m2 DB 'Enter your second name (max. 9 characters):' ,0DH, 0AH, '$'
m3 DB 'Your name is: ','$'
A DB 10, 21 DUP (?)
B DB 10, 21 DUP (?)
C DB 18, 21 DUP (?)
.CODE
.STARTUP
LEA DX, m1
MOV AH, 09H
INT 21H
MOV AH, 0AH
LEA DX, A
INT 21H
LEA DX, CRLF
MOV AH, 09H
INT 21H
LEA DX, m2
MOV AH, 09H
INT 21H
MOV AH, 0AH
LEA DX, B
INT 21H
LEA DX, CRLF
MOV AH, 09H
INT 21H
MOV Ch,A[2]
MOV C[2], CH
MOV Ch,A[3]
MOV C[3], CH
MOV Ch,A[4]
MOV C[4], CH
MOV Ch,A[5]
SECTION: A 8
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
MOV C[5], CH
MOV Ch,A[6]
MOV C[6], CH
MOV Ch,A[7]
MOV C[7], CH
MOV Ch,A[8]
MOV C[8], CH
MOV Ch,A[9]
MOV C[9], CH
MOV BH, 00H
MOV BL, A[1]
ADD BL, 2
MOV C[BX], 20H
MOV Ch,B[2]
MOV C[BX+1], CH
MOV Ch,B[3]
MOV C[BX+2], CH
MOV Ch,B[4]
MOV C[BX+3], CH
MOV Ch,B[5]
MOV C[BX+4], CH
MOV Ch,B[6]
MOV C[BX+5], CH
MOV Ch,B[7]
MOV C[BX+6], CH
MOV Ch,B[8]
MOV C[BX+7], CH
MOV Ch,B[9]
MOV C[BX+8], CH
LEA DX, m3
MOV AH, 09H
INT 21H
LEA DX, CRLF
MOV AH, 09H
INT 21H
MOV CH,A[1]
MOV CL,B[1]
ADD CL,CH
ADD CL, 1
MOV C[1],CL
MOV AH, 09H
MOV BH, 00H
MOV BL, C[1]
MOV C[BX+2],'$'
LEA DX, C[2]
INT 21H
ret
SECTION: A 9
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
Output Result:
Figure 4 & 5: Output of program
SECTION: A 10
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
Objective#2: Prompt for the institute name from the user (MAX 8 characters), then prompt
for an index (1-8), then display the modified string with your CMS ID.
Program:
Source Code:
org 100h
.MODEL SMALL
.STACK 100H
.DATA
CRLF DB 0DH, 0AH, '$'
m1 DB 'Enter the input (max. 8 characters):' ,0DH, 0AH, '$'
m2 DB 'Enter a number (1-8):' ,0DH, 0AH, '$'
m3 DB ,0DH, 0AH, 'Incorrect Input!!!$'
A DB 14, 16 DUP (?)
.CODE
.STARTUP
LEA DX, m1
MOV AH, 09H
INT 21H
MOV AH, 0AH
LEA DX, A
INT 21H
MOV dl, A[1]
ADD DL, 30H
CMP DL, 38H
JA LA1
LEA DX, CRLF
MOV AH, 09H
INT 21H
LEA DX, m2
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
MOV CL, A[1]
ADD CL, 30H
CMP CL, AL
JB LA1
MOV CH, A[1]
ADD CH, 5
MOV A[1], CH
SUB AL, 30H
MOV BH, 00H
MOV BL, AL
MOV DL, A[BX+2]
MOV A[BX+6], DL
MOV A[BX+1],35H
MOV DL, A[BX+3]
MOV A[BX+7], DL
SECTION: A 11
LAB MANUAL EMBEDDED SYSTEMS Name: Sameer Ahmed
CMS ID: 50246
MOV A[BX+2], 30H
MOV DL, A[BX+4]
MOV A[BX+8], DL
MOV A[BX+3], 32H
MOV DL, A[BX+5]
MOV A[BX+9], DL
MOV A[BX+4], 34H
MOV DL, A[BX+6]
MOV A[BX+10], DL
MOV A[BX+5], 36H
LEA DX, CRLF
MOV AH, 09H
INT 21H
MOV AH, 09H
MOV BH, 00H
MOV BL, A[1]
MOV A[BX+2],'$'
LEA DX, A[2]
INT 21H
MOV AH, 0
INT 21H
LA1:
MOV AH, 09H
LEA DX, m3
INT 21H
ret
Output Result:
Figure 6 & 7: Output of program
Conclusion:
In this lab we learned what are registers, their classification and how to create an array in
80X86 Emulator, how to take a string input modify it and show the output on the screen.
SECTION: A 12