Professional Documents
Culture Documents
Chp4 Assembly Langguage
Chp4 Assembly Langguage
ASSEMBLY LANGUAGE
What is an Assembler?
What is a Compiler?
1
1.5 Editor
An assembly language can be written using any text editor and save in a
computer as a text file. Example: EDIT, notepad, etc.
1.6 Debugger
A debugger is a program that allows you to examine registers and memory and
to step through a program one statement at a time to see what is going on. In
assembly language, you will depend upon this ability to see what the CPU is
doing.
2.3 REGISTERS
Within the CPU are high–speed storage areas, which are directly linked to the
control unit and the arithmetic logic unit. Because of this, instructions using only
registers execute more quickly than do instructions using conventional memory.
The Intel instruction set requires the use of at least one register for nearly all
instructions.
Registers are special work areas inside the CPU, designed to be accessed at
high speed. The registers are 16 bits long, but you have the option of
accessing the upper or lower halves of the four data registers: AX, BX, CX
and DX
Type of registers
1. Data Registers
AX, BX CX and DX
Example:
16-bit AX register
AH register AL register
(8 bit) Upper bits (8 bits) lower
bits
Instructions may address either 16-bit or 8-bit data registers from the
following list:
2
AX BX CX DX
AH AL BH BL CH CL DH DL
2. Segment registers
The CPU contains four segment registers, used as base locations for
program instructions, data , and the stack.
3. Index Registers
3
Index registers contain the offsets of variables. The term offsets refers to the
distance of a variable, label, or instruction from its base segment Index
registers speed up processing of strings, array, and other data structures
containing multiple elements. The index registers are:
SI (source index). This register takes its name from the string movement
instructions, in which the source string is pointed to by the SI register. SI
usually contains an offset value from the DS register, but it can address
any variable.
4. Special Registers.
5. Flags Register
The flags register is a special 16-bit register with individual bit positions
assigned to show the status of the CPU or the results of arithmetic operations.
Each relevant bit position is given a name; other positions are undefined.
Bit Position
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
x x x x O D I T S Z x A x P x C
O = Overflow S = Sign
D = Direction Z = Zero
I = Interrupt A = Auxiliary Carry
T = Trap P = Parity
x = undefined C = Carry
4
A flag or bit is set when it equals 1 ; it is clear (or reset) when it equals 0.
The CPU sets flags by turning on individual bits in the Flags register. There
are two basic types of flags : control flags and status flags.
Flags
Control Flags
Individual bits may be set in the Flags register by the programmer to
control the CPU’s operation. These are the Direction, Interrupt, and
Trap flags
The Trap flag determines whether or not the CPU will be halted after
each instruction. Debuggers set this flag so programmers can single-
step (trace) through their programs one instruction at a time. The
flags values are 1 = trap on and , 0 = trap off; the flag may be set
by the INT 3 instruction.
Status Flags
The Status flag bits reflect the outcome of arithmetic and logical
operations performed by the CPU. These are the Overflow, Sign,
Zero, Auxiliary Carry, Parity, and Carry flags.
5
The Sign flag is set when the result of an arithmetic or logical
operation generates a negative result. Since a negative number always
has a 1 in the highest bit position, the Sign flag is always a copy of
the destination’s sign bit. The flag values are 1 = negative, 0 =
positive.
The Auxiliary Carry flag is set when an operation causes a carry from
bit 3 to bit 4 (or borrow from bit 4 to bit 3) of an operand. It is
rarely used by the programmer. The flag values are 1 = carry, and
0 = no carry.
The Parity flag reflects the number of bits in the result of an operation
that are set. If there is an even number of bits, the Parity is even. If
there is an odd number of bits, the Parity is odd. This flag is used by
the operating system to verify memory integrity and by
communications software to verify correct transmission of data.
A variable is a symbolic name for a location in memory where some data are
stored. In assembly language, variables are identified by labels. Each label
shows the starting location of a variable.
Example:
Declaration
aList db ‘ABCD’
So if the first letter were at offset 0, the next letter would be at offset 1, the next
at 2, and so on.
6
Description Bytes Attribute
DB Define byte 1 byte
DW Define word 2 word
DD Define doubleword 4 doubleword
The DB directives allocates storage for one or more 8-bit values. The following
syntax diagram shows that name is optional, and only one initialvalue is required.
If more are supplied, they must be separated by commas:
Syntax:
Initialvalue can be one or more 8-bit numeric values, a string constant, a constant
expression, or a question mark (?).
If the value is signes, it has a range of -128 to +127; if unsigned, the range is 0
to 255.
Examples:
Multiple values
A list of 8-bit numbers may be grouped under a single label, with the values
separated by commas.
Example:
Note:
7
When a hexadecimal number begins with a letter (A - F), a leading zero is
added to prevent the assembler from interpreting it as a label.
A variables contents may be left undefined by using the question mark (?). Or
numeric expression can initialize a variable with a value that is calculated at
assembly time.
Example : count db ?
ages db ?, ? , ?, ?,?
size db 10*20
Syntax:
The assembler reverses the bytes in a word value when storing it in memory; the
lowest byte occurs at the lowest address. When the variable is moved to a 16-bit
register, the re-reverses the bytes.
8
Storage : B6 2A
Syntax :
Offset: 00 01 02 03
Value: 78 56 34 12
i. MOV Instruction
Because MOV copies data from one operand to another, it is called a data
transfer instruction.
Syntax :
The data are really copied, so the source operand is not changed. The source may
be an immediate value, a register, or a memory operand. The destination may be
a register or a memory operand.
Register Operands. A move involving only registers is the most efficient. Any
register may be used as a source operand, and any register except CS and IP
may be destination operands
Direct Operands. A variable may be one (but not both) of the operands in a
MOV instruction. The contents of the variable are either the source or destination
of the move.
Examples of MOV with all three types of operands are shown here:
9
mov dx , cx ; 16-bit register to register
mov bl , 1 ; 8-bit immediate to register
mov bx , 8FE2h ; 16-bit immediate to register
mov al , count ; 8-bit memory to register
mov total , ax ; 16-bit register to memory
mov total, 1000h ; 16-bit immediate to memory
.
.
.
count db 10
total dw 0
Operands do have a few limitations, which are based on both the physical design
of the processor and its instruction set. The following are not allowed:
mov ip , ax ; IP destination
mov ds , 1000h ; immediate to segment
mov ds , es ; segment to segment
mov al , bx ; mismatching operands sizes
mov word_1 , al ; mismatching operands sizes
mov 1000h , ax ; immediate destination
mov word_2 , word_1 ; memory to memory
.
.
.
word_1 dw 1000h
word_2 dw 0
10
The two operands may be registers or memory operands, as long as one operand
is a register.
XCHG is the most efficient way to exchange two 8-bit or 16-bit operands
because you don’t need a third register or variable to hold a temporary value.
Paticularly in sorting applications, this instruction provides a speed advantage.
One or both operands may be registers, or a register may be combined with a
memory operand. Two memory operands may not be used together. The
following are all valid examples:
INC destination
DEC destination
11
add al, 1 ; add immediate value to 8-bit
register
add cl, al ; add 8-bit register to register
add bx, 1000h ;add immediate data to 16-bit register
add var1, ax ; add 16-bit register to memory
add dx, var1 ; add 16-bit memory to register
add var1, 10 ; add immediate value to memory
The sizes of the two operands must match, and only one may be a
memory operand. A segment register may not be the destination
operand. On the bit level, what really happens is that the source operand
is negated and then added to the destination. For example , 4 - 1 is
really 4 + ( -1). Recall that twos complement notation is used for
negative numbers, so -1 is stored as 11111111b:
0 0 0 0 0 1 0 0 (4)
+ 1 1 1 1 1 1 1 1 (-1)
0 0 0 0 0 0 1 1 (3)
As a matter of fact, this addition resulted in a carry out of the highest bit (setting
the Carry flag), but the carry is ignored when working with signed numbers.
Examples:
If either ADD or SUB generates a result of zero, the Zero flag is set; if the
result is negative, the Sign flag is set. In the following example, line 2
generates a result of zero and line 4 generates a result of -1 (FFFFh).
12
mov ax , 10
sub ax , 10 ; AX = 0 , ZF = 1
mov bx , 1
sub bx , 2 ; BX = FFFF , SF = 1
The Zero flag is set when the result of an arithmetic operation equals 0. Note that
INC and DEC affect the Zero flag, but not the Carry flag:
mov bl , 4Fh
add bl , 0B1h ; BL = 00
mov ax, 0FFFFh
inc ax ; NO CARRY, ZF = 1
Unsigned Operations.
For unsigned arithmetic, we are only concerned with the Carry flag. If the result
of an addition operation is too large for the destination operand, the carry flag is
set. For example, the sum of 0FFh + 1 should equal 100h; but only the two
lowest digits (00) fit into AL. The operation sets the Carry flag:
i. AND instruction
Performs the boolean AND operation using 8-bit or 16-bit operands and
places the result in the destination operand. The syntax is:
13
The operand must be the same size and only one of them may be a memory
operand. For each matching bit in the two operand, the following rule applies:
if both bits equal 1, the result bit is 1; otherwise, it is 0. Example of AND
operation:
0 0 1 1
0 1 0 1
0 0 0 1
ii. OR instruction
Performs the boolean OR operation using 8-bit or 16-bit operands and places
the result in the destination operand. The syntax is:
OR Destination, Source
The operand must be the same size and only one of them may be a memory
operand. For each matching bit in the two operand, the following rule applies:
For each matching bit in the two operand, the following rule applies: if both
bits equal 0, the result bit is 0; otherwise, it is 1. Example of OR operation:
0 0 1 1
0 1 0 1
0 1 1 0
The operand must be the same size and only one of them may be a memory
operand. For each matching bit in the two operand, the following rule applies:
If both bits are the same(both 0 or both 1) the result is 0; otherwise the result
is 1. Example of XOR operation:
0 0 1 1
0 1 0 1
0 1 1 0
The NOT instruction reverses all the bits in an operand, changing 1 to 0 and
vice versa. The result is called 1st compliment. The syntax is:
NOT Destination
CPU stores negative number in 2nd compliment form. You can create the 2 nd
compliment of a number using the NOT and INC instruction.
14
v. NEG instruction
The NEG instruction converts a number from positive to negative and vice
versa. It does by converting the number to its 2nd compliment: The syntax is:
NEG Destination
The instruction reverses all bits in the destination operand and add 1 to the
result.
Perform the comparison operation of two 8-bit or 16-bit operands. The result
reflected in the state of the flags register. The CMP perform an implied
subtraction of the source operand from the destination operand, but neither
operand is actually changed. The syntax is:
Only one operand may be a memory operand. The destination can be neither
an immediate operand nor the IP register. Neither operand may be a
segment register. The following table shows how the flags registers affected
by the CMP instruction.
15
ii. Jump and Conditional Jump
i. LOOP instruction
LOOP destination
First , the LOOP instruction subtracts 1 from CX. Then, if CX is greater than
zero, control transfers to destination.
Jcond destination
Each conditional jump instruction checks one or more flsgs, returning a result
of true or false. If the result true, the jump is taken: otherwise, the program
does nothing and continue to the next instruction.
cmp ax, bx
je label1
.
.
Int 21
16
Example of conditional jump
i. MUL Instructions
MUL source
IMUL source
mov al, 6h
mov bl, 10h
mul bl ;AX = 0060h
17
Example 2. Multiply AX by 10h:
Result = -16
For an 8-bit multiplication, IMUL sets the Carry and Overflow flags if the
resulting AH is not a sign extension of AL (i.e., if the product is larger than
8 bits)
For a 16-bit multiplication, the Carry and Overflow flags are set if DX is not
a sign extension of AX ( the product is larger than 16 bits).
mov al, 48
mov bl, 4
imul bl ;AX = 00C0h +192), CF = 1, OF = 1
18
Example 2: 8-bit Operation : -4 x 4 = -16
mov al , -4
mov bl , 4
imul bl ;AX = FFF0h(-16), CF = 0, OF = 0
mov ax , 48
mov bx , 4
imul bx ;DX=0000, AX=00C0h(+192),CF = 0,OF = 0
iii. DIV
The DIV and IDIV instructions perform both 8- and 16-bit division, signed
and unsigned. A single operand is supplied (register or memory operand),
which is assumed to be the divisor. Syntax:
DIV source
IDIV source
AX Operand AL AH
DX:AX Operand AX DX
19
mov bl , 2 ; divisor
div bl ; AL = 41h, AH = 01h
Example 2. 16-bit division(8003h/100h = 80h, remainder 3). After the
division, the quotient is in AX and the remainder is in DX.
The shift and rotate instruction provide a way to move bits around in and
operand.
i. SHL instruction
The SHL (shift left) instruction shifts each bit in a destination operand to the
left, filling the lowest bit with 0. The highest bit is moved to the carry flag, and
the bit that was in the carry flag is lost.
0
CF
SHL Destination, 3
SHL Destination, cl
shl dl, 1 ; dl = 1
shl dl, 1 ; dl = 2
shl dl, 1 ; dl = 4
shl dl, 1 ; dl = 8
shl dl, 1 ; dl = 16
shl dl, 1 ; dl = 32
20
ii. SHR instruction
The SHR (shift right) instruction shifts each bit in a destination operand to the
right, filling the highest bit with 0. The lowest bit is moved to the carry flag, and
the bit that was in the carry flag is lost.
0
CF
There are two formats for SHL,
SHR Destination, 3
SHR Destination, cl
Division: One of the most powerful uses of SHR instruction is for performing
high-speed division. The division must always be a power of 2, and the
number of times you shift is actually the exponent. For example, shifting right
one position is the same as dividing by 2 1, shifting 2 to left is same as dividing
by 22, and so on. The example shows the decimal value of DL after each shift.
SAL (shift arithmetic left) and SAR (shift arithmetic right) are shift instruction
for sign number. SAL is identical to SHL and included in the instruction set
only for completeness. SAR shift each bit to the right and makes a copy of the
sign bit.
CF
SAR copies the lowest bit of the destination operand into the carry flag, shift
bit of operand 1 position to the right and duplicate the original sign bit.
The syntax for SAR and SHR is identical to that of SHL and SHR:
SAR Destination, 3
SAR Destination, cl
21
iv. ROL instruction
The ROL (rotate left) instruction moves each bit to the left. The highest bit is
copied both into Carry flag and into the lowest bit.
CF
ROL Destination, 3
ROL Destination, cl
v. ROR instruction
The ROR (rotate right) instruction moves each bit to the right. The lowest bit is
copied into the Carry flag and into the highest bit.
CF
The ROL (rotate/left) instruction moves each bit to the left. The highest bit is
copied both into Carry flag and into the lowest bit.
ROL Destination, 3
ROL Destination, cl
22
vi. RCL and RCR instruction
The RCL (rotate carry left) instruction shifts each bit to the left and copies the
highest bit into the Carry flag. The carry flag is copied into the lowest bit of the
operand.
CF
RCL Destination, 3
RCL Destination, cl
The RCR (rotate carry right) instruction shifts each bit to the right and copies
the lowest bit into the Carry flag. The carry flag is copied into the higest bit of
the operand.
CF
RCR Destination, 3
RCR Destination, cl
23