Opena, Neil Piolo Tristian L
Opena, Neil Piolo Tristian L
LABORATORY 01
INTRODUCTION TO ASSEMBLY LANGUAGE
The system bus (shown in yellow) connects the various components of a computer.
The CPU is the heart of the computer, most of computations occur inside the CPU.
RAM is a place to where the programs are loaded in order to be executed.
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.
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 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):
d8 - stays for 8 bit signed immediate displacement (for example: 22, 55h, -1) d16 -
stays for 16 bit signed immediate displacement (for example: 300, 5517h, -259).
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 can be inside or outside of the [ ] symbols, assembler generates the same machine code
for both ways.
Displacement is a signed value, so it can be both positive or 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 = 0100H, BX = 0030H, SI = 0070H.
The following addressing mode: [BX + SI] + 25H
Is calculated by processor to this physical address: 0100H * 10H + 30H + 70H + 25H =
010C5H. or
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.
All valid combinations can be formed by taking only one item from each column or skipping the column
by not taking anything from it. BX and BP never go together. Neither SI and DI do.
Examples of valid addressing modes:
[BX+5] [BX+SI] [DI+BX-4]
The value in segment register (CS, DS, SS, ES) is called a segment, and the value in general
purpose register (BX, SI, DI, 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 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
In order to say the compiler about data type, these prefixes should be
used: byte ptr - for byte. word ptr - for word (two bytes). for example:
In certain cases the assembler can calculate the data type automatically.
MOV INSTRUCTION
- copies the second operand (source) to the first operand (destination).
- the source operand can be an immediate value, GPR or memory location
- the destination register can be a GPR or memory location
- both operands must be the same size, which can be a byte or a word.
where:
REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
memory: [BX], [BX+SI+7], variable immediate: immediate: 5, -24, 3Fh,
10001101b
For segment registers only these types of MOV are supported:
where:
The MOV instruction cannot be used to set the value of the CS and IP registers.
Actually the above program writes directly to video memory, now we can see that MOV is a very
powerful instruction.
VARIABLES
Variable is a memory location. For a programmer it is much easier to have some value be kept in a
variable named "var1" then at the address 5A73:235B, especially when you have 10 or more variables.
Our compiler supports two types of variables: BYTE and WORD.
name DB value
name DW value
where:
DB - stays for Define Byte.
DW - stays for Define Word.
name - can be any letter or digit combination, though it should start with a letter. It's possible to
declare unnamed variables by not specifying the name (this variable will have an address but
no name).
value - can be any numeric value in any supported numbering system (hexadecimal, binary,
or decimal), or "?" symbol for variables that are not initialized.
1 ORG 100h
2
3 MOV AL, var1
4 MOV BX, var2
5
6 RET ; stops the program.
7 VAR1 DB 7
8 var2 DW 1234h
Questions:
1. Encode in the emulator the above
code: Paste your screenshot code here.
3. What happens if code line 8 and 9 will be transferred to line 3 and 4, respectively, offsetting
line 3 and 4
5. Write a code that will store 4 variables, two are of one byte long and the
other two are 1 word long, to the 4 general purpose registers. Copy paste
your code here.
Screenshot your register values after running all the lines of the code.
When the DB variable is 16 bits, the error “overflowing” appears because the
variable can only support 8 bits. The same goes for the DW, which should be
16 bits.
Prepared by: Engr. Kirk Vincent V. Montinola, ECE
BATANGAS STATE UNIVERSITY BatstateU Alangilan
8. What does MOV function do?
Because it moves data from one location to another, the MOV instruction is the
most crucial command in the 8086. The data in the source is copied to the
destination by MOV.
It shows that it can be used to access any element's value in the array. It was
once used to combine many registers in order to acquire different memory
locations.