Lab Session 3: Arithmetic Operations in Assembly Language
Lab Session 3: Arithmetic Operations in Assembly Language
Lab Session 3: Arithmetic Operations in Assembly Language
LAB SESSION 3
THEORY
Assembly language is a low level programming language. You need to get some knowledge
about computer structure in order to understand anything. The simple computer model as I see it:
8086 CPU has 8 general purpose registers, each register has its own name:
Despite the name of a register, it's the programmer who determines the usage for each general
purpose register. The main purpose of a register is to keep a number (variable). The size of the
above registers is 16 bit, it's something like: 0011000000111001b (in binary form), or 12345 in
decimal (human) form.
4 general purpose registers (AX, BX, CX, DX) are made of two separate 8 bit registers, for
example if AX= 0011000000111001b, then AH=00110000b and AL=00111001b. Therefore,
when you modify any of the 8 bit registers 16 bit register is also updated, and vice-versa. The
same is for other 3 registers, "H" is for high and "L" is for low part.
Because registers are located inside the CPU, they are much faster than memory. Accessing a
memory location requires the use of a system bus, so it takes much longer. Accessing data in a
register usually takes no time. Therefore, you should try to keep variables in the registers.
Register sets are very small and most registers have special purposes which limit their use as
variables, but they are still an excellent place to store temporary data of calculations.
b) Segment registers
Although it is possible to store any data in the segment registers, this is never a good idea. The
segment registers have a very special purpose - pointing at accessible blocks of memory.
Segment registers work together with general purpose register to access any memory value. For
example if we would like to access memory at the physical address 12345h (hexadecimal), we
should set the DS = 1230h and SI = 0045h. This is good, since this way we can access much
more memory than with a single register that is limited to 16 bit values.
CPU makes a calculation of physical address by multiplying the segment register by 10h and
adding general purpose register to it (1230h 10h + 45h = 12345h):
IP register always works together with CS segment register and it points to currently executing
instruction.
Flags register is modified automatically by CPU after mathematical operations, this allows to
determine the type of the result, and to determine conditions to transfer control to other parts of
the program.
Generally you cannot access these registers directly, the way you can access AX and other
general registers, but it is possible to change values of system registers using some tricks that
you will learn a little bit later.
MEMORY ACCESS
To access memory we can use these four registers: BX, SI, DI, BP combining these registers
inside [ ] symbols, we can get different memory locations. These combinations are supported
(addressing modes):
Micro-Processor Systems Lab session 03
Department of Electrical Engineering
d8 stays for 8 bit signed immediate displacement (for example: 22, 55h, -1, etc...)
d16 - stays for 16 bit signed immediate displacement (for example: 300, 5517h, -259,
etc...)
Displacement can be a immediate value or offset of a variable, or even both. If there are
several values, assembler evaluates all values and calculates a single immediate value.
Displacement is a signed value, so it can be both positive and negative. Generally the
compiler takes care about difference between d8 and d16, and generates the required
machine code.
For example, let's assume that DS = 100, BX = 30, SI = 70.The following addressing mode: [BX
+ SI] + 25 is calculated by processor to this physical address: 100 * 16 + 30 + 70 + 25 = 1725.
By default DS segment register is used for all modes except those with BP register, for
these SS segment register is used.
There is an easy way to remember all those possible combinations using this chart:
You can form all valid combinations by taking only one item from each column or skipping the
column by not taking anything from it. As you see BX and BP never go together. SI and DI also
don't go together. Here are examples of valid addressing modes:
The value in segment register (CS, DS, SS, and ES) is called a segment.
The value in purpose register (BX, SI, DI, and BP) is called an offset.
When DS contains value 1234h and SI contains the value 7890h it can be also recorded
as 1234:7890. The physical address will be 1234h * 10h + 7890h = 19BD0h. If zero is added to a
Micro-Processor Systems Lab session 03
Department of Electrical Engineering
decimal number it is multiplied by 10, however 10h = 16, so if zero is added to a hexadecimal
value, it is multiplied by 16, for example:
7h = 7
70h = 112
EXAMPLE#1
Multiply
OUTPUT
output :
-- H L
AX
EXAMPLE#2
DIVIDE
OUTPUT
-- H L
AX
Micro-Processor Systems Lab session 03
Department of Electrical Engineering
EXAMPLE#3
name add-sub
org 100h
add bl, al
sub bl, 1
ret
LAB TASK
1) Fill the output blanks of Example#1 &Example#2?
2) Attached the Outputs of Example#3, by performing it first by (i)add-sub, (ii)add &
(iii)sub?
3) Perform Example#2 again by using ax=100 & bx= your own roll.no & attach axoutput
value
CONCLUSION
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________