You are on page 1of 11

The Hong Kong Polytechnic University

Department of Electrical and Electronic Engineering

EIE3343 Computer Systems Principles

Laboratory Exercise 1: 80x86 registers and memory architecture

Student Name: ___Cheung Kin Chung__

Student No.: __23117382D______

Date: ____1/3/2024_________

(Note: Unless otherwise specified, all numbers are in hexadecimal format.)

Part A: Registers and Memory

Start the emu8086 program and open “mycode0.asm”. Start the emulation. Ensure
you have written permissions to the folder where you will save your project.

Display the current status of the CPU and the current contents of the registers.

Enter “12” and “34” in higher and lower bytes at AX. Examine it in the extended
value viewer by double clicking on any byte of AX (you can also activate it using
“view”  “extended value viewer”). Make sure that “word” is selected. The content
of register AX is changed to 1234.

Enter “2000” at DS. The content of the register DS is changed to 2000.

Enter “DS:1000” in the memory dump window address. Examine the memory
segment.

Double-click the memory location DS:1000 address at the memory dump window.
Select “word” instead of “byte” in the extended value viewer. Overwrite the contents
of the memory location DS:1000 with 107F. Check that the memory location has been
updated with the new data. From the memory dump window, determine the
endianness of the 8086 (little or big Endian).

Answer:

Little Endian

1
In the extended value viewer, jump to address “1FFF:0010” and write “FFFF” to the
contents of the first two memory locations. Jump to memory location 2000:0000 at
the memory dump window. Do they match with each other? Try other numbers,
“1234” and “5678”. Based on your observation, conclude if one can refer to the exact
memory location with different segment and offset address combinations. Calculate
the physical address for both cases.

Answer:

Yes, the physical address=1FFF0+10=20000+0 = 20000

Part B: Entering machine code, single stepping, and breakpoint

Exit emu8086 and re-start emu8086 again with mycode0.asm opened.

Start the emulator to display the current status of the CPU. What would be the next
instruction to be executed? How can you know?

Answer:
CS:IP 0720:0000,so the next instruction to be executed is 07200+0000=07200
The instruction is mov ax, 0710H

You may input a program for execution in two ways. In the first approach, you input
the machine code of the program to the code segment directly.

Enter “CS:0100” at the memory dump window address to display a sequence of the
instructions in the code segment. Then double-click on the “CS:0100” memory
address to enter the machine code directly

2
Input a stream of bit patterns to “CS:0100” in order of “31 C0 B4 76 B0 23 00 C4”
(this data can be entered as words or bytes). A machine code program is entered into
the code segment by doing this. You may disassemble the program into assembly
language instructions corresponding to the code patterns by clicking on the first
memory address just edited. Record the disassembled assembly code(s).

Answer:

xor ax, bx
mov ah, 076h
mov al, 023h
add ah, al
xor ax, bx
mov ah, 076h
mov al, 023h
add ah, al
xor ax, bx
mov ah, 076h
mov al, 023h
add ah, al
xor ax, bx
mov ah, 076h
mov al, 023h
add ah, al

3
Execute the machine code just entered step by step. Ensure the IP's content is set to
100H before tracing the program. Record the contents of the registers AX and IP
before executing the program and after each step.

Answer:

Step AH AL AX IP
Before Step 1 00 00 0000 100
After Step 1 00 00 0000 102
After Step 2 76 00 7600 104
After Step 3 76 23 7623 106
After Step 4 99 23 9923 108

Exit emu8086 and restart it again with mycode0.asm.

Now, we will enter a program using the second approach. This is done by typing the
following assembly program after the comment “add your code here” in the text
editor window of emu8086. (It is not necessary to enter the comments). Save the
program as “mycode1.asm”.

MOV AX,0FF00h ; I1
ADD AX,01F0h ; I2
MOV BX,AX ; I3
NEG BX ; I4
ADD AX,BX ; I5

Press the emulate button to observe the machine code corresponding to the entered
assembly program. Record the corresponding machine code for each CS address.

Answer:

IP Instruction Machine Code

4
0007 I1 B800FF
000A I2 05D001
000D I3 8BD8
000F I4 F7DB
0011 I5 03C3

Restart the execution of the program by clicking on the ‘reload’ button. Trace the
program by repeatedly clicking the ‘single step’ button until the final instruction you
entered is executed. Keep tracking the contents of the registers (AX and BX) and the
status flags. Explain the function of the program line by line.

Answer:

Step AX BX CF ZF SF OF PF AF Remarks

Before
0710 0000 0 0 0 0 0 0
Step I1
initialize AX with
FF00H
Step I1 FF00 0000 0 0 0 0 0 0

[AX]+01F0AX
Step I2 00F0 0000 1 0 0 0 1 0

Copy AX to BX
Step I3 00F0 00F0 1 0 0 0 1 0

2s’ complement of
Step I4 00F0 FF10 1 0 1 0 0 0 [BX]

Step I5 0000 FF10 1 1 0 0 1 0 [AX]+[BX]AX

Reload the program. This time, we do not trace the program step by step. We specify
where the program should stop before we execute the program. This is done by setting
a breakpoint.

This is done by clicking on the instruction or memory address where we want to insert
the breakpoint at the memory address 07233h and selecting debugset breakpoint
from the top menu. You can run the program until the breakpoint by clicking the ‘run’
button instead of using the ‘single step’ one.

Part C: Memory addressing

5
Restart emu8086 with mycode2.asm and enter the following code as explained in the
previous exercise. Save the revised program.
MOV AX,33 ; I1
MOV AX,[SI]  ; I2
MOV AX,2[SI]  ; I3
MOV AX,[SI+2] ; I4
MOV AX,[SI]+2 ; I5
MOV AX,[0fh]  ; I6
MOV AX,[BX+SI-3]  ; I7
MOV AX,20[BP+SI-16]  ; I8

Various addressing modes are used in these instructions. Determine which addressing
mode is used in which instruction.

6
----------------------------------------------------------------------------------
; multi-segment executable file template mycode2.asm.

data segment
; add your data here!

ends

stack segment
dw 128 dup(0) ; initialize 128 words
; with 00
ends

code segment
start:
mov ax, cs
mov ds, ax ; initialize DS
add ax, 1h
mov ss, ax ; initialize SS
mov bx, 04 ; initialize BX with 04h
mov si, 02 ; initialize SI with 02h
mov bp, 06 ; initialize BP with 06H

; add your code here

mov ax, 4c00h ; exit to operating system.


int 21h
ends

end start ; set entry point and stop the assembler.


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

7
Answer:

The physical
The The addressing
address of the
Step AX BX SI BP segment mode of the
source
used source operator
operand

Before
0721 0004 0002 0006
Step I1

N\A immediate
Step I1 0021 0004 0002 0006 None

07202
Step I2 D88E 0004 0002 0006 DS Register indirect

07204
Step I3 0105 0004 0002 0006 DS Indexed relative

07204
Step I4 0105 0004 0002 0006 DS Indexed relative

07204
Step I5 0105 0004 0002 0006 DS Indexed relative

07204
Step I6 06BD 0004 0002 0006 DS Direct

0720F
Based indexed
Step I7 05D8 0004 0002 0006 DS
relative

07203 Based indexed


Step I8 8B02 0004 0002 0006 DS
relative

8
Disassemble the program. You will find that some of the instructions are
disassembled identically. Which instructions are they? Which conclusion can you
draw based on your observation?

Answer:

MOV AX, 2[SI]


MOV AX, [SI+2]
MOV AX, [SI]+2
They have same machine code

Part D: Examine the flag register more closely

Restart emu0886 with mycode0.asm and enter the following code in the new program
window; save the program as “mycode3.asm”.
MOV AX, 034F6h
MOV BX, 065FBh
ADD AX, BX

Trace the program, show how the ADD instruction affects the flag, and explain why.
Show the computation of the addition in binary format.

Answer:
34F6 0011 0100 1111 0110
+65FB +0110 0101 1111 1011
9AF1 1001 1010 1111 0001

CF = __0(NC)____ ;Why? There is no carry beyond d15


ZF = __0(NZ)____ ;Why? The result is non-zero
SF = __1(NG)____ ;Why? The sign bit is 1
OF = ___1(OV)___ ;Why? Overflow occurs, a carry from d14 but no carry from
d15
PF = ___0(PO)___ ;Why? There is odd number of 1s in the lower byte
AF = ___1(AC)___ ;Why? There is a carry from d3 to d4

Are there any changes in the flag bits after the MOV instruction? Why?

Answer:

9
No change as MOV instruction don’t affect flag bits.

Change the two numbers to be added to AAAAH and 5556H and show how the flags
are affected. Save the program as “mycode4.asm”.

Answer:
AAAA 1010 1010 1010 1010
+5556 +0101 0101 0101 0110
0000 0000 0000 0000 0000

CF = __1(CY)____ ;Why? There is a carry beyond d15


ZF = ___1(ZR)___ ;Why? The result is 0
SF = ____0(PL)__ ;Why? The sign bit is 0
OF = ____0(NV)__ ;Why? No overflow occur
PF = ____1(PE)__ ;Why? There is even number of 1s in the lower byte
AF = ____1(AC)__ ;Why? There is a carry from d3 to d4

Part E: Examine the operation of the stack

Restart emeu8086 with mycode0.asm and enter the following code. Save the program
as “mycode5.asm”.
MOV BX,22h ; I1
MOV AX,33h ; I2
PUSH AX ; I3
PUSH BX ; I4
MOV AX,0 ; I5
MOV BX,0 ; I6
POP AX ; I7
POP BX ; I8

Trace the program and record the values of the AX, BX, SP and the Stack contents
after executing each instruction.

Answer:

After Stack content at


executing the top
AX BX SP
each high byte, low
instruction byte
I1 0710 0022 0100 10, D8
I2 0033 0022 0100 10, D8
I3 0033 0022 00FE 00, 33
I4 0033 0022 00FC 00, 22
I5 0000 0022 00FC 00, 22
I6 0000 0000 00FC 00, 22
I7 0022 0000 00FE 00, 33

10
I8 0022 0033 0100 10, D8
(Note: You can use “view”  “memory” to examine the content of the stack SS:SP.)
Modify the program such that the modified program can restore the contents of AX
and BX after execution.

Answer:

POP BX
POP AX
at the back of program

- End -
Lawrence Cheung
January 2024

11

You might also like