Anda di halaman 1dari 31

E-laboratory Tool for Microprocessor Assembly Language

Programming and its Applications




Fundamental of Microprocessor

Lab 0 Introduction to Intel 8086 Assembly Language

Introduction
Most programmers shy away from Assembler (or assembly language). People tend to
consider it as a very difficult language to understand and use. This tutorial is the first in a series
that will attempt to dismiss these misgiving about Assembler. It will demonstrate the tools that
greatly simplify the authoring of Assembler, and show you how to integrate for microprocessor
based project applications.
First, what is Assembler? Assembler is the language of the processor. You can't get
any lower level than this (except for possibly the actual byte values of the instructions). Every
command (MOV, ADD, and so forth) is translated by an Assembler program directly into a
number that, in turn, is fed to the processor on execution.
The advantage of using Assembler over other languages is speed. Even with the modern
compiler's ability to optimize code, the code that it produces would have trouble competing with
the same written and optimized by hand in Assembler.
Each processor has its own assembly language (syntax base on architecture of that
microprocessor), and that is the main reason why one assembly program written for one
microprocessor may not work with different microprocessor from different architecture class i.e.
Intel program written for Windows operating system is not going to work with Mac running on
Motorola microprocessor.
Intel 8086 registers and flags
Intel 8086 uses different types of register and flags for arithmetic operations and program
flow control, but we will only talk about the most important registers and flags that will be
essential for our introductory programming course.
USE RESOURCES FROM EMU8086 to write a brief description for each of the register and
flag below:
General data register:
AX AH / AL

BX BH / BL
CX CH / CL

DX DH / DL

SI

DI

SP

BP

IP

Flag registers (PSW) :



1. Carry Flag (CF) - this flag is set to 1 when there is an unsigned overflow. For example
when you add bytes 255 + 1 (result is not in range 0...255). When there is no overflow
this flag is set to 0.
2. Parity Flag (PF) - this flag is set to 1 when there is even number of one bits in result,
and to 0when there is odd number of one bits.
3. Auxiliary Flag (AF) - set to 1 when there is an unsigned overflow for low nibble (4
bits).
4. Zero Flag (ZF) - set to 1 when result is zero. For non-zero result this flag is set to 0.
5. Sign Flag (SF) - set to 1 when result is negative. When result is positive it is set to 0.
(This flag takes the value of the most significant bit.)
6. Trap Flag (TF) - Used for on-chip debugging.
7. Interrupt enable Flag (IF) - when this flag is set to 1 CPU reacts to interrupts from
external devices.
8. Direction Flag (DF) - this flag is used by some instructions to process data chains, when
this flag is set to 0 - the processing is done forward, when this flag is set to 1 the
processing is done backward.
9. Overflow Flag (OF) - set to 1 when there is a signed overflow. For example, when you
add bytes100 + 50 (result is not in range -128...127).
Instruction type : use EMU8086 tutorial to find out how these instruction work and write a
short description about each of the below instruction.
http://www.emu8086.com/assembler_tutorial /reference.html
Data movement instructions are used to move or redirect data from register to register/memory
MOV -
PUSH -
POP -
LEA -
Arithmetic and Logic instructions are instructions that manipulate data arithmetically or
logically
ADD -
SUB -
INC -
DEC -
AND -
OR -
XOR -
NOT -
SHL -
SHR -
Control Flow instructions are used to control the flow of the program. The x86 processor
maintains an instruction pointer (IP) register that is a 32 bit value indicating the location in the
memory where the current instruction starts. Normally, it increments to point to the next
instruction in memory begins after execution of instruction. The IP register cannot be
manipulated directly, but it is updated implicitly by provided control flow instructions.
When looking up these instructions, it is imperative to note how each of the condition take
advantage of the different flag to achieve its operation.
JMP -
JE -
JNE -
JZ -
JG -
JGE -
JL -
JLE -
Analysis
Q1. If you have to write a program to count a number stored in AL register from 1 10, how
would you do that? Just show simple drawing/writing of how your program would look in
English.

Example: We call this code Pseudo code and its just an idea of how you would attack the
problem without thinking too much about the syntax of a program.
PROGRAM TO ADD 1 to 14:

MOVE 01h to AL register
MOVE 0Eh (14 in decimal) to BL register
ADD BL to AL (result will be stored in AL)


Fundamental of Microprocessor

Lab 1: 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, or 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. Examples are:
ADD src, dest ADD AX, CX
SUB dest, src SUB AX, BX
MUL arg MUL CX
SAMPLE program for arithmetic operation:

ORG 0100H
MOV AL,X ; Move X into AL
MOV BL,02H ; Move 2 into BL
MUL BL ; Multiply AL by BL, the result will be in AX
MOV CX,AX ; Store the answer in CX

MOV AL,Y ; Move Y into AL
MOV BA,05H ; Move 5 into BL
MUL BL ; Multiply AL by BL, the result will be in AX


ADD AX,CX ; ADD first arithmetic result to second one

MOV CL,W ; Move the dividend (W) into CL
DIV CL ; Divide CL into AX, the result will be in AH,AL

RET

X DB 04H
Y DB 03H
W DB 02H

Procedure

Complete the following tasks.

1. Launch the EMU8086 and enter the program provided in the above section.

2. Try to run the program. It contains a bug (more exactly a typo), which is so trivial that
even with no prior experience you should be able to correct it. Fix and re-assemble the
program until no errors or warnings are left.

3. After the program execute without any error, check the status and content of the registers
after each single step execution.

Analysis
Q1. What is the different between ADD and MUL operation?
Q2. What would be a situation when you use DB or DW as a data type declaration?
Q3. What happens to the value in each register after each execution?
Q4. What happens to IP (Instruction pointer) after each execution?
Q5. What happens, if you change the value on line #3 from 02H to 0FFFH? Why?


Fundamental of Microprocessor

Lab 2: Data Movement and Arithmetic Operation (Continued)

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?

Fundamental of Microprocessor

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

mov bx,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)
div bx ;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 of
program, but only 1 byte for the 3
rd
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?


Fundamental of Microprocessor

Lab 4: Subroutine (Continued)

In this lab we will continue working with concept of assembly language subroutine. We also
going to introduce a new logical operation SHL and SHR
SHL is simply shift left. In other words, say you've got the value 1 in AL. In binary that's
00000001. If you SHL that 1 times, you get a value of 2 in AL or 00000010. Do it again and
your bit(s) shift up by 1 more.

SHL is cool because it's a quick way to multiply (amongst other things) a value by 2,4,8, etc
because every time you SHL you double the value.

SHR shift right will half your original value, making quick division a snap.

Doing a "shl ax, cl" just takes the value in CL and shifts AX that many times.
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. After the first single step execution write down the value to AL in decimal and binary
(i.e. 3 = 00000011)
5. Write down value of AL after the shl instruction got executed in decimal and binary.
6. Did you notice what happen to the value compare to before shl instruction ?
7. Finish program execution and note the final value of AX (AH and AL)
8. What is the final value of AX in decimal? (remember that value shown in EMU is a hex
value)
9. What is the relationship between original AL value and final AX value?
10. Can you guess what is the purpose of this program? Also can you write a comment after
each line of code?












org 100h


START: mov ax,x ; Please help write the commend for
; for this program
call x10
call x10
call x10

ret

x10 PROC

shl ax,1
mov bx,ax
shl ax,1
shl ax,1
add ax,bx
ret
ENDP
x dw 5



Analysis
Q1. What if you change SHL AX, 1 SHL AX, 2, what would happen to the result of the
program and why?
Q2. Can you write a program that will give you a square of a number by using SHL
command?


Fundamental of Microprocessor

Lab 5: Binary to Gray Converter

Gray code:
The problem with natural binary codes is that, with real (mechanical) switches, it is very unlikely
that switches will change states exactly all the time. For example, in the two states (binary and
gray) shown below, all three switches change state. In the brief period while all are changing, the
switches will read some spurious position. Even without keybounce, the transition might look
like 011 001 101 100. When the switches appear to be in position 001, the observer
cannot tell if that is the "real" position 001, or a transitional state between two other positions. If
the output feeds into a sequential system (possibly via combinational logic) then the sequential
system may store a false value.
The Gray Code (reflected binary code) solves this problem by changing only one switch at a
time, so there is never any ambiguity of position
Dec Gray Binary
0 000 000
1 001 001
2 011 010
3 010 011
4 110 100
5 111 101
6 101 110
7 100 111
Notice that state 7 can roll over to state 0 with only one switch change. This is called the "cyclic"
property of a Gray code. In the standard Gray coding the least significant bit follows a repetitive
pattern of 2 on, 2 off ( 11001100 ); the next digit a pattern of 4 on, 4 off; and so forth.





Hardware implementation:

Exclusive OR gates were used to convert between Gray and Binary and Binary and Gray. What
is the main different between the above circuits? Now we will implement Gray to Binary and
Binary to Gray conversion using assembly language for 8086. Microprocessor is just a big
combination of different logic gate, so we can leverage the benefit of having those logic gates
simulate any hardware implementation of circuit via software.

Procedure

Complete the following tasks.

1. Launch the EMU8086.
2. Copy and Paste below code into EMU8086 code windows
3. Single step execution and note content of AX and BX registers after each instruction
4. Note value of BX after shr instruction
5. Note the value of AX after program finish instruction and compare the value to the binary
to gray conversion table. Did you get the right converted value?











; Binary to gray converter

org 0100h

mov bx, x ; store number in bx register
mov ax, bx ; copy content of bx to ax
shr bx, 1 ; shift bx on binary position
; to the right
xor ax, bx ; XOR shifted bx to ax to get
; result in gray code

ret


x dw 000bh


Analysis
Q1. Can you visualize and compare the software implementation and hardware implementation
of Binary to Gray converter?
Q2. Which Binary to Gray conversion is the easier of the two conversions?
Q3. Can you now attempt to write an assembly program to convert the given Gray coded number
to Binary?
Hint: step 1: fix the left most digit as the same
step 2: then add the digits from right to left
org 0100h

START: mov ax, gray ; store Gray coded number in ax
mov bx, ax ; copy value to bx (we do this
; so we can have a fixed number
; to work with.
shr ax, 1 ; shift ax to the right 1 bin
LOOP1: xor bx, ax ; XOR shifted ax to original(bx)
shr ax, 1 ; keep shifting ax to the right
; until the end of number
jnz LOOP1 ; We know when we come to the
; end by zero flag
mov ax, bx

RET

gray dw 0FE2Bh

Fundamental of Microprocessor

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.


org 0100h

START: mov cl, 03h

LOOP_JNZ: dec cl
jnz LOOP_JNZ


mov bl, 04h
mov al, 04h
LOOP_JZ: dec al
dec bl
xor bl, al
jz LOOP_JZ


mov bl, 02h
mov al, 06h
LOOP_JG: dec al
cmp al, bl
jg LOOP_JG


mov bl, 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?


Fundamental of Microprocessor

Lab 7: Flags and Control Flow (Continued)

Pairs of ONEs
Given a binary 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?














org 0100h

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 0s in a 16 bit string?


Fundamental of Microprocessor

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









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

org 0100h

BCDBIN: xor ax,ax ; clear ax by xor it w/ itself
mov dx, BCD
mov ch, 4 ; counter is 4 digit

X10: shl ax, 1 ; 10x routine by using shl
mov bx, ax
mov cl, 2
shl ax, cl
add ax,bx

ADDDIGIT: mov cl, 4 ; specify how many bits to shift
rol dx, cl ; roll bit left by "cl" bit = 4 bits
mov bx,dx
and bx, 000fh ; mask off our shifted DX
add ax,bx

dec ch
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?

Fundamental of Microprocessor

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?


Fundamental of Microprocessor

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?

Fundamental of Microprocessor

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:

in al, 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?

Anda mungkin juga menyukai