Fundamental of Microprocessor
Lab 2: Data Movement and Arithmetic Operation
Data Movement Instructions
These instructions are used to move data from one place to another. These places can be registers, memory, or even inside peripheral devices. Some examples are:
MOV AX, 13
MOV BX, AX
Arithmetic Instructions
Arithmetic instructions take two operands: a destination and a source. The destination must be a register or a memory location. The source may be either a memory location, a register, or a constant value. Note that at least one of the two must be a register, because operations may not use a memory location as both a source and a destination. Some examples are:
ADD src, dest ADD ax, cx
SUB dest, src SUB ax, bx
MUL arg MUL cx
Procedure
Complete the following tasks.
1. Launch the EMU8086.
2. Use the data movement and arithmetic operations from the last lab to complete the following program.
a. Convert 50 °F to degree °C, using following formula:
°C = (°F - 32) x 5/9
b. Convert 98 °C to °F, using following formula:
°F = °C x 9/5 + 32
2. After the program execute without any error, check the status and content of the registers after each single step execution.
ORG 0100H
MOV AL,F ; Load given F into AL
MOV BL,20H ; Load 32 (20h) into BL
SUB AL,BL ; Subtract 32 from F
MOV BL,05H ; Move 5 into BL
MUL BL ; Multiply AL by BL (5)
MOV BL,09H ; Move 9 into BL
DIV BL ; Divide AL by BL
RET
F DB 50
ORG 0100H
MOV AL,C ; Load given C into AL
MOV BL, 09H ; Load 9 into BL
MUL BL ; Multiply BL with AL ( 9 x 5)
MOV BL,20H ; Load 32 (20h) into BL
ADD AL,BL ; add BL with AL
MOV BL, 05H ; Load 5 into BL
DIV BL ; Divide AL by BL ((Cx9)+ 32)/5
RET
C DB 98
Analysis
Q1. Can you use 32d (decimal) instead of 20h (hex) in the program? Try it !
Q2. What if you have to do a lot of conversion, is there any better way or faster way of doing it?
Lab 3: Subroutine and Stack Operation
Subroutine
A subroutine is a set of code that can be branched to and returned from such a way that the code is as if it were inserted at the point from which it is branched to.
The branch to a subroutine is referred to as CALL and the corresponding branch back is known as RETURN
Subroutine provide the primary means of breaking the code in a program to modules
Stack
- It is a section of memory sets aside for storing return addresses.
- It is also used to save the contents of registers for the calling program while a procedure
executes
- Another use of stack is to hold data or addresses that will be acted upon by a procedure.
- PUSH and POP are 2 instructions that can operate on stack
Procedure
Complete the following tasks.
1. Launch the EMU8086.
2. We will be writing a program to calculate the sum of all resistance in a series and parallel circuit.
3. Copy and paste the code into EMU 8086 and run the program in single step mode
4. Make sure you open the "stack" window (should be on the bottom of emulator windows).
5. After each single step execution, notice the content of AX, BX, CX, DX, IP, Stack
Instructions used in this program: write the syntax for each of these instruction
MOV - POP -
ADD - DIV -
PUSH -
MUL -
org 100h
Start: mov dx, r5 ;r5 = dl
add dx, r6 ;r5+r6 = dl
push dx ;save value of dx in stack
mov cx, r4 ;r4 = dl
push cx
add cx, dx ;r4 + (r5+r6)
call PARALLEL
PARALLEL PROC
movbx,cx ;mov cx (r4) + (r5+r6) into bx
pop cx ;pop old cx (r4) back to cx
pop dx ;pop (r5+r6) back to dx
mov ax, cx ;mov old cx(r4) to ax
mul dx ;multiply ax with dx (r4 *
;(r5+r6)
divbx ;final division [(r4 *
;(r5+r6)]/[(r4 + (r5+r6)]
ENDP
r4 DW 100d
r5 DW 500d
r6 DW 600d
Analysis
Q1.What happens to the value in IP when you execute the program?
Q2.Why did IP increase by 5 bytes (0100 to 0104 and 0105 to 0108) for the first 2 lines ofprogram, but only 1 byte for the 3rd line of program (push dx → 0108 to 0109)?
Q3.What happens to the value in the stack after each push operation?
Q4.When you use POP instruction, which value come out in what order?
Q5.Can you just POP the value into any of the register?
Q6.Why do you want to use subroutine in your program?
Lab 6: Flags and Control flow
Assembly program will execute the code linearly down the memory address until it encounter program control flow instruction (JMP, JNZ, JG .....). We can use these program control flow instructions to manipulate our program to branch out to different sections based on the condition of the flags that has been set by previous arithmetic or logical operations.
Sample control flow structure for simple timer program
Procedure
Complete the following tasks.
1. Launch the EMU8086.
2. Copy and Paste below code into EMU8086 code windows
3. Single step execution with the flags windows open
4. Note value of AX,BX,CX after each execution
5. Look up each of the control instruction on EMU8086 tutorial and write down the condition of the branch. Compare those condition to the value of the flags when the jump happen.
org0100h
START: mov cl, 03h
LOOP_JNZ: dec cl
jnz LOOP_JNZ
movbl, 04h
mov al, 04h
LOOP_JZ: dec al
decbl
xorbl, al
jz LOOP_JZ
movbl, 02h
mov al, 06h
LOOP_JG: dec al
cmp al, bl
jg LOOP_JG
movbl, 06h
mov al, 00h
LOOP_JL: inc al
cmp al, bl
jl LOOP_JL
ret
Analysis
Q1.Can you write a program that use all of the branch conditions from EMU8086 tutorial and study how they work?
Lab 7: Flags and Control Flow
Pairs of ONEs
Given abinary number 11001011. Write a program that count how many pairs (consecutive) of 1 are in this binary string. For example, 11111111 has 7 total pair of 1, and 01101111 has 4 total pair of 1.
HINT: Use combination of knowledge from last 2 previous labs about flags, program flow instruction and binary shift operation to help you with designing this program.
SHL →
JZ →
JNC →
JNS →
JS →
Procedure
Complete the following tasks.
1. Launch the EMU8086.
2. Copy and Paste below code into EMU8086 code windows
3. Single step execution with the flags windows open
4. Note value of AX and flags after each execution, and which operation cause value
of AL to behave the way it did?
5. What happens to the value of DX (more specifically DL) after each jump?
org0100h
MOV ax,x ; Move binary string into AX
START: SHL ax,1 ; Shift content of AX to the left on
; position
JZ END ; The end of the string jump to END
JC CALL PAIR_ONE ; If the Carry flag is 1, then call
; PAIR_ONE subroutine
JNC START ; If the Carry flag is 0, then start
; the program again
ADD_ONE: INC dx ; INC DX after both condition are met
JMP START ; Back to start and do it again
END: NOP ; This is the end of the main program
ret
PAIR_ONE PROC ; Subroutine for comparison
; after first stage (CF=1) is
; met.
JS CALL add_one ; If sign flag is a 1, then both
; condition are met. Jump to
; ADD_ONE:
JNS START ; If sign flag is a 0, then back
; to START to do it all over
; again
RET
ENDP
x DW 0FFE6h ; Define string of 16 bits
; binary number to be checked
Analysis
Q1.Is there any other alternate way (flags) we can write this program?
Q2.Can you write a program that will give out the number of pair of 0's in a 16 bit string?
Lab 8: BCD to Binary
BCD:Short for Binary Coded Decimal, BCD is also known as packet decimal and is numbers 0 through 9 converted to four-digit binary. Below is a list of the decimal numbers 0 through 9 and the binary conversion.
Decimal to BCD conversion table:
Decimal
|
BCD
|
0
|
0000
|
1
|
0001
|
2
|
0010
|
3
|
0011
|
4
|
0100
|
5
|
0101
|
6
|
0110
|
7
|
0111
|
8
|
1000
|
9
|
1001
|
Using this conversion, the number 25, for example, would have a BCD number of 0010 0101 or 00100101. However, in binary, 25 is represented as 11001.
Covert following decimal numbers to BCD
24 → 1298 →
125 → 7 →
365 → 534 →
Convert following hex to binary
9 → 290A →
1E → DDC →
FFF → ABC →
Did you notice the pattern? Decimal is closely related to BCD (very easy to convert between the two) and the same can be said for Hexadecimal and Binary.
Binary to BCD Shift and Add-3 Algorithm
1. Shift the binary number left one bit.
2. If 8 shifts have taken place, the BCD number is in the Hundreds, Tens, and Units column.
3. If the binary value in any of the BCD columns is 5 or greater, add 3 to that value in that BCD column.
4. Go to 1.
CONVERT: 0Eh to BCD
Operation
|
Tens
|
Units
|
Binary
|
HEX |
|
|
|
Start |
|
|
1 1 1 0
|
Shift 1 |
|
1
|
1 1 0
|
Shift 2 |
|
11
|
10
|
Shift 3 |
|
111
|
0
|
Add 3 |
|
1 0 1 0
|
0
|
Shift 4 |
1
|
0 1 0 0
|
|
BCD |
1
|
4
|
|
Procedure
Complete the following tasks.
1. Launch the EMU8086.
2. Look up and research usage of "ROL" instruction
3. Copy and Paste below code into EMU8086 code windows
4. Single step execution with the flags windows open
5. The code is for BCD to Binary converter
6. Follow the single step and watch the value of the registers after each operation
org0100h
BCDBIN: xorax,ax ; clear ax by xor it w/ itself
mov dx, BCD
movch, 4 ; counter is 4 digit
X10: shl ax, 1 ; 10x routine by using shl
movbx, ax
mov cl, 2
shl ax, cl
addax,bx
ADDDIGIT: mov cl, 4 ; specify how many bits to shift
rol dx, cl ; roll bit left by "cl" bit = 4 bits
movbx,dx
andbx, 000fh ; mask off our shifted DX
addax,bx
decch
jnz x10
ret
BCD dw 6789h
Analysis
Q1.Can you come up with the algorithm use for BCD to Binary converter after watching the program run?
Q2.Can you write a program to convert Binary to BCD using the shift and add 3 algorithm?
Q3.What did "ROL" do to our program when it got executed?
Lab 9: Arithmetic Operation (revisit)
Procedure
Write programs to do following arithmetic operation in EMU 8086 and execute them using single step mode.
AL = 12h
BL = 13h
1) AL + BL
2) BL - AL
3) AL * BL
4) AL / BL
AX = 0123h
BX = 2000h
DX = 0FF3h
1) AX + DX
2) AX - BX
3) AX * DX
4) AX / DX
5) Negate BX
6) Compare AX and DX
Analysis
Q1. What are the main differences between first set of operation and second set of operation?
Q2. Did you notice what happens to the remainder in the division operation?
Q3. What is the syntax for multiplication and division?
Are they different from addition/subtraction? How?
Q4. What happens to the flags when you do subtraction in the second set?
Q5. What happens to the (all) flags when you do NEG operation?
Q6. What happens to the (all) flags when you do CMP operation?
Lab 10: Connect to Outside World Applications
Input and Output (I/O) in 8086 Assembly Language
Each microprocessor provides instructions for I/O with the devices that are attached to it, in this lab we will just talk about OUT instruction.
OUT → Output from AL or AX to port.
First operand is a port number. If required to access port number over 255 - DX register should be used.
Example:
MOV AX, 0FFFh ; turn on all the light (or turn all binary to 1)
OUT 4, AX ; output content of AX to port 4
EMU8086 provide us with the virtual LED (output display). We can use this simple LED virtual output to test how to write a simple program that send output to the outside world.
Procedure
1. Open EMU8086
2. Copy and paste the following code into the code editor part of EMU8086
3. Single step execute the code and watch the register and flags
4. Keep LED display windows where you can see how the display changes
#start=led_display.exe#
#make_bin#
name "led"
mov ax, 1234
out 199, ax
mov ax, -5678
out 199, ax
; loop to write
; values to port up to specify
; number (in this case it is 0010h):
mov ax, 0
x1:
out 199, ax
inc ax
cmp ax, 0FFFEh
jmp x1
ret
Analysis
Q1. What do you have to change if we want to count all the way up to 255(decimal)?
Q2. Can you think of any practical purpose for program like this in our real life applications?
Lab 11: Connect to Outside World Applications (Continued)
Input and Output (I/O) in 8086 Assembly Language
Each microprocessor provides instructions for I/O with the devices that are attached to it, in this lab we will just talk about OUT instruction.
OUT → Output from AL or AX to port.
First operand is a port number. If required to access port number over 255 - DX register should be use.
Example:
MOV AX, 0FFFh ; turn on all the light (or turn all binary to 1)
OUT 4, AX ; output content of AX to port 4
This short program for emu8086 shows how to keep constant temperature using heater and thermometer (between 60° to 80°). It is assumed that air temperature is lower 60°.
Procedure
1. Open EMU8086
2. Copy and paste the following code into the code editor part of EMU8086
3. Execute the code (not in single step mode) and watch the running code display
4. Keep thermometer display where you can see.
#start=thermometer.exe#
; temperature rises fast, thus emulator should be set to run at the maximum speed.
; if closed, the thermometer window can be re-opened from emulator's "virtual devices" menu.
#make_bin#
name "thermo"
; set data segment to code segment:
mov ax, cs
mov ds, ax
start:
inal, 125
cmp al, 60
jl low
cmp al, 80
jle ok
jg high
low:
mov al, 1
out 127, al ; turn heater "on".
jmp ok
high:
mov al, 0
out 127, al ; turn heater "off".
ok:
jmp start ; endless loop
Analysis
Q1. How did this program monitor the temperature?
Q2. What port number is used to control the heater?