Sunteți pe pagina 1din 22

4345 Assembly Language

Assembly Language

Dr. Esam Al_Qaralleh


CE Department
Princess Sumaya University for Technology

Assembly Language 3-1

Overview of Assembly Language

Advantages:

9 Faster as compared to programs written using high-level languages


9 Efficient memory usage
9 Control down to bit level

Disadvantages:
Need to know detail hardware implementation
Not portable
Slow to development and difficult to debug

Basic components in assembly Language:

Instruction, Directive, Label, and Comment

Assembly Language 3-2


Example of Assembly Language Program

;NUMOFF.ASM: Turn NUM-LOCK indicator off. Comments


.MODEL SMALL
.STACK
Assembly directive
.CODE
.STARTUP
MOV AX,40H ;set AX to 0040H
D1: MOV DS,AX ;load data segment with 0040H Instructions
MOV SI,17H ;load SI with 0017H
AND BYTE PTR [SI],0DFH ;clear NUM-LOCK bit
.EXIT
Assembly directive
END

Label
Assembly Language 3-3

The Big (Simplified) Picture


Machine code
010000101010110110
High-level code 101010101111010101
101001010101010001
101010101010100101
char *tmpfilename; 111100001010101001
int num_schedulers=0; 000101010111101011

ASSEMBLER
int num_request_submitters=0;
int i,j; 010000000010000100
000010001000100011
if (!(f = fopen(filename,"r"))) {
xbt_assert1(0,"Cannot open file %s",filename);
101001010010101011
} 000101010010010101
while(fgets(buffer,256,f)) { 010101010101010101
if (!strncmp(buffer,"SCHEDULER",9))
num_schedulers++; 101010101111010101
if (!strncmp(buffer,"REQUESTSUBMITTER",16)) 101010101010100101
num_request_submitters++;
}
111100001010101001
fclose(f);
tmpfilename = strdup("/tmp/jobsimulator_

Assembly code
sll $t3, $t1, 2
add $t3, $s0, $t3
sll $t4, $t0, 2
add $t4, $s0, $t4 Program counter register
lw $t5, 0($t3) register
register

CPU
lw $t6, 0($t4)
slt $t2, $t5, $t6

COMPILER
beq $t2, $zero, endif
add $t0, $t1, $zero
sll $t4, $t0, 2 Control
add $t4, $s0, $t4 ALU Unit
lw $t5, 0($t3)

Assembly Language lw
slt
$t6,
$t2,
0($t4)
$t5, $t6
3-4
beq $t2, $zero, endif
The Big (Simplified) Picture
Hand-written Machine code
High-level code Assembly code 010000101010110110
101010101111010101
101001010101010001
101010101010100101
sll $t3, $t1, 2
char *tmpfilename; 111100001010101001
int num_schedulers=0; add $t3, $s0, $t3
000101010111101011

ASSEMBLER
int num_request_submitters=0; sll $t4, $t0, 2
int i,j; 010000000010000100
add $t4, $s0, $t4
lw $t5, 0($t3)
000010001000100011
if (!(f = fopen(filename,"r"))) {
xbt_assert1(0,"Cannot open file %s",filename); lw $t6, 0($t4) 101001010010101011
} slt $t2, $t5, $t6 000101010010010101
while(fgets(buffer,256,f)) { 010101010101010101
if (!strncmp(buffer,"SCHEDULER",9))
beq $t2, $zero, endif
num_schedulers++; 101010101111010101
if (!strncmp(buffer,"REQUESTSUBMITTER",16)) 101010101010100101
num_request_submitters++;
}
111100001010101001
fclose(f);
tmpfilename = strdup("/tmp/jobsimulator_

Assembly code
sll $t3, $t1, 2
add $t3, $s0, $t3
sll $t4, $t0, 2
add $t4, $s0, $t4 Program counter register
lw $t5, 0($t3) register
register

CPU
lw $t6, 0($t4)
slt $t2, $t5, $t6

COMPILER
beq $t2, $zero, endif
add $t0, $t1, $zero
sll $t4, $t0, 2 Control
add $t4, $s0, $t4 ALU Unit
lw $t5, 0($t3)

Assembly Language
lw
slt
$t6,
$t2,
0($t4)
$t5, $t6
3-5
beq $t2, $zero, endif

What we do in this class:


Hand-written Machine code
High-level code Assembly code 010000101010110110
101010101111010101
101001010101010001
101010101010100101
sll $t3, $t1, 2
char *tmpfilename; 111100001010101001
int num_schedulers=0; add $t3, $s0, $t3
000101010111101011

ASSEMBLER
int num_request_submitters=0; sll $t4, $t0, 2
int i,j; 010000000010000100
add $t4, $s0, $t4
lw $t5, 0($t3)
000010001000100011
if (!(f = fopen(filename,"r"))) {
xbt_assert1(0,"Cannot open file %s",filename); lw $t6, 0($t4) 101001010010101011
} slt $t2, $t5, $t6 000101010010010101
while(fgets(buffer,256,f)) { 010101010101010101
if (!strncmp(buffer,"SCHEDULER",9)) beq $t2, $zero, endif
num_schedulers++; 101010101111010101
if (!strncmp(buffer,"REQUESTSUBMITTER",16)) 101010101010100101
num_request_submitters++;
}
111100001010101001
fclose(f);
tmpfilename = strdup("/tmp/jobsimulator_

Assembly code
sll $t3, $t1, 2
add $t3, $s0, $t3
sll $t4, $t0, 2
add $t4, $s0, $t4 Program counter register
lw $t5, 0($t3) register
register

CPU
lw $t6, 0($t4)
slt $t2, $t5, $t6

COMPILER
beq $t2, $zero, endif
add $t0, $t1, $zero
sll $t4, $t0, 2 Control
add $t4, $s0, $t4 ALU Unit
lw $t5, 0($t3)

Assembly Language
lw
slt
$t6,
$t2,
0($t4)
$t5, $t6
3-6
beq $t2, $zero, endif
Software Model for the 8086

Assembly Language 3-7

Overview
Intel 8088 facts
20 bit address bus allow accessing VDD (5V)
1 M memory locations
16-bit internal data bus and 8-bit
external data bus. Thus, it need 20-bit
8-bit data
address
two read (or write) operations to
read (or write) a 16-bit datum control
8088 control

signals
Byte addressable and byte-swapping

signals
To 8088 from 8088
Word: 5A2F
CLK

18001 5A High byte of word


GND
18000 2F Low byte of word

8088 signal classification


Memory locations
Assembly Language 3-8
Organization of 8088/8086
Address bus (20 bits)
AH AL General purpose
BH BL register
CH CL
Execution Unit
DH DL
(EU) Data bus
SP CS (16 bits)
Segment
BP register DS
SI SS
DI ALU Data bus ES
(16 bits)
IP

Bus
control
ALU Instruction Queue External bus
EU
control
Flag register
Bus Interface Unit (BIU)
Assembly Language 3-9

The 8086 Registers


To write assembly code for an ISA (Instruction Set
Architecture) you must know the name of registers
Because registers are places in which you put data to perform
computation and in which you find the result of the computation
(think of them as variables)
The registers are really numbered, but assembly languages give
them easy-to-remember names
The 8086 offered 16-bit registers
Four general purpose 16-bit registers
AX
BX
CX
DX

Assembly Language 3-10


The 8086 Registers
AX BX CX DX

AH AL BH BL CH CL DH DL

Each of the 16-bit registers consists of 8 low bits and 8


high bits
Low: least significant
High: most significant

The ISA makes it possible to refer to the low or high


bits individually
AH, AL
BH, BL
CH, CL
Assembly Language 3-11
DH, DL

The 8086 Registers


AX BX CX DX

AH AL BH BL CH CL DH DL

The xH and xL registers can be used as 1-byte


register to store 1-byte quantities
Important: both are tied to the 16-bit register
Changing the value of AX will change the values of
AH and AL
Changing the value of AH or AL will change the value
of AX

Assembly Language 3-12


The 8086 Registers

Assembly Language 3-13

The 8086 Registers


Two 16-bit index registers:
SI
DI
These are basically general-purpose registers
But by convention they are often used as
pointers, i.e., they contain addresses
And they cannot be decomposed into High and
Low 1-byte registers

Assembly Language 3-14


The 8086 Registers
Two 16-bit special registers:
BP: Base Pointer
SP: Stack Pointer
Well discuss these Later
Four 16-bit segment registers:
CS: Code Segment
DS: Data Segment
SS: Stack Segment
ES: Extra Segment
Well discuss them later as well

Assembly Language 3-15

The 8086 Registers


The 16-bit Instruction Pointer (IP) register:
Points to the next instruction to execute
The 16-bit FLAGS registers
Information is stored in individual bits of the FLAGS
register
Whenever an instruction is executed and produces a
result, it may modify some bit(s) of the FLAGS register
Example: Z (or ZF) denotes one bit of the FLAGS
register, which is set to 1 if the previously executed
instruction produced 0, or 0 otherwise

Assembly Language 3-16


The 8086 Registers
AH AL = AX
BH BL = BX
CH CL = CX
DH DL = DX

SI
DI

BP
SP
IP
= FLAGS

CS
DS
SS
ES

16 bits

Control
ALU
Assembly Language
Unit 3-17

General Purpose Registers


15 8 7 0
AX AH AL Accumulator

BX BH BL Base
Data Group
CX CH CL Counter

DX DH DL Data

SP Stack Pointer

BP Base Pointer
Pointer and
Index Group
SI Source Index

DI Destination Index

Assembly Language 3-18


Arithmetic Logic Unit (ALU)
A B F Y
n bits n bits
0 0 0 A+B
0 0 1 A -B
Carry
0 1 0 A -1
Y= 0 ? F 0 1 1 A and B
1 0 0 A or B
A>B?
1 0 1 not A

Y
Signal F control which function will be conducted by ALU.
Signal F is generated according to the current instruction.

Basic arithmetic operations: addition, subtraction,


Basic logic operations: and, or, xor, shifting,
Assembly Language 3-19

Flag Register
Flag register contains information reflecting the current status of a
microprocessor. It also contains information which controls the
operation of the microprocessor.
15 0
OF DF IF TF SF ZF AF PF CF

Control Flags Status Flags


IF: Interrupt enable flag CF: Carry flag
DF: Direction flag PF: Parity flag
TF: Trap flag AF: Auxiliary carry flag
ZF: Zero flag
SF: Sign flag
OF: Overflow flag

Assembly Language 3-20


Instruction Machine Codes
Instruction machine codes are binary numbers
For Example:

1000100011000011 MOV AL, BL


Register
MOV
mode

Machine code structure

Opcode Mode Operand1 Operand2

Some instructions do not have operands, or have only one operand


Opcode tells what operation is to be performed.
(EU control logic generates ALU control signals according to Opcode)
Mode indicates the type of a instruction: Register type, or Memory type
Operands tell what data should be used in the operation. Operands can
be addresses telling where to get data (or where to store results)
Assembly Language 3-21

EU Operation

1. Fetch an instruction from instruction


queue AH AL General purpose
BH BL register
2. According to the instruction, EU control CH CL
logic generates control signals. DH DL
(This process is also referred to as instruction SP
BP
decoding)
SI
DI ALU Data bus
3. Depending on the control signal, (16 bits)
EU performs one of the following
operations:
An arithmetic operation ALU
EU
A logic operation control instruction
Flag register 1011000101001010
Storing a datum into a register
Moving a datum from a register
Changing flag register

Assembly Language 3-22


Generating Memory Addresses
How can a 16-bit microprocessor generate 20-bit memory addresses?

Left shift 4 bits


FFFFF

16-bit register 0000 Addr1 + 0FFFF


Segment
+ 16-bit register Offset
Addr1
Offset
(64K)

20-bit memory address Segment


address
00000

Intel 80x86 memory address generation 1M memory space

Assembly Language 3-23

Code, Data, Stack


A program constantly references all three
regions
Therefore, the program constantly
address space

references bytes in three different segments code


For now lets assume that each region is fully
contained in a single segment, which is in fact
not always the case
CS: points to the beginning of the code data
segment
DS: points to the beginning of the data
segment
SS: points to the beginning of the stack stack
segment

Assembly Language 3-24


Memory Segmentation
A segment is a 64KB block of memory starting from any 16-byte
boundary
For example: 00000, 00010, 00020, 20000, 8CE90, and E0840 are all valid
segment addresses
The requirement of starting from 16-byte boundary is due to the 4-bit
left shifting

Segment registers in BIU


15 0
CS Code Segment

DS Data Segment

SS Stack Segment

ES Extra Segment
Assembly Language 3-25

Memory Address Calculation

Segment addresses must be stored Segment address 0000


in segment registers
Offset is derived from the combination + Offset

of pointer registers, the Instruction Memory address


Pointer (IP), and immediate values

Examples

CS 3 4 8 A 0 SS 5 0 0 0 0
IP + 4 2 1 4 SP + F F E 0
Instruction address 3 8 A B 4 Stack address 5 F F E 0

DS 1 2 3 4 0
DI + 0 0 2 2
Data address 1 2 3 6 2
Assembly Language 3-26
Fetching Instructions
Where to fetch the next instruction?
8088 Memory

CS 1234
IP 0012 12352 MOV AL, 0

12352

Update IP
After an instruction is fetched, Register IP is updated as follows:

IP = IP + Length of the fetched instruction

For Example: the length of MOV AL, 0 is 2 bytes. After fetching this instruction,
the IP is updated to 0014

Assembly Language 3-27

Accessing Data Memory


There is a number of methods to generate the memory address when
accessing data memory. These methods are referred to as
Addressing Modes
Examples:
Direct addressing: MOV AL, [0300H]

DS 1 2 3 4 0 (assume DS=1234H)
0 3 0 0
Memory address 1 2 6 4 0

Register indirect addressing: MOV AL, [SI]

DS 1 2 3 4 0 (assume DS=1234H)
0 3 1 0 (assume SI=0310H)
Memory address 1 2 6 5 0

Assembly Language 3-28


In-class Exercise

Consider the byte at address 13DDE within


a 64K segment defined by selector value
10DE. What is its offset?

Assembly Language 3-29

In-class Exercise

Consider the byte at address 13DFE within


64K segment defined by selector value
10DE. What is its offset?

13DDE = 10DE * 1610 + offset


offset = 13DFE - 10DE0
offset = 301E (a 16-bit quantity)
Assembly Language 3-30
Address Computation Example
Consider the whole 1MB address space
Say that we want a 64K segment whose end is 8K from the end of the
address space
The address at the end of the address space is FFFFF
8K in binary is 1000000000000, that is 01000 in hex
So the address right after the end of the segment is
FFFFF - 01000 + 1 = FEFFF + 1 = FF000
The length of the segment is 64K
64K in binary is 1000000000000000, that is 08000
So the address at the beginning of the segment is
FF000 - 08000 = F7000
So the value to store in a segment register is F700
To reference the 43th byte in the segment, one must store 002A (= 4210) in
an index register
The address of that byte is: F7000 + 002A = F702A
The address of the last byte in the segment is: F7000 + 07FFF = FEFFF
Which is right before FF000, the beginning of the last 8K of the address space
Assembly Language 3-31

Instructions Format and


Compilation

Assembly Language 3-32


Developing software for the personal computer
.ASM file

;NUMOFF.ASM: Turn NUM-LOCK indicator off.


; All characters following a ; till the line end
.MODEL SMALL are comments, ignored by the assembler
.STACK Assembler reserved words
.CODE
Assembly language instructions
.STARTUP
MOV AX,40H ;set AX to 0040H
MOV DS,AX ;load data segment with 0040H
MOV SI,17H ;load SI with 0017H
AND BYTE PTR [SI],0DFH ;clear NUM-LOCK bit
.EXIT
END

Assembly Language 3-33

Developing software for the personal computer


.ASM file

;NUMOFF.ASM: Turn NUM-LOCK indicator off.


;
.MODEL SMALL
Register pair (16 bit) (destination of MOV)
.STACK Hexadecimal value to be loaded (source for MOV)
.CODE Prepare the Data Segment
Data Segment register pair
.STARTUP
MOV AX,40H ;set AX to 0040H
MOV DS,AX Source Index ;load data segment with 0040H
MOV SI,17H ;load SI with 0017H
AND BYTE PTR [SI],0DFH ;clear NUM-LOCK bit The complete address
of the byte containing
.EXIT
Second operand for logical AND NumLock bit is
END
(immediate hexadecimal value) specified.
First operand and destination for ANDing with DFH=1101.1111B,
logical AND Memory address Assembly Language only b5 (bit 5) of specified memory
3-34
specified by DS and SI together. location is affected (reset to 0)
1.7 Developing software for the personal computer
.LST file
Memory location addresses
Machine language codes generated by the assembler
;NUMOFF.ASM: Turn NUM-LOCK indicator off.
;
.MODEL SMALL
.STACK
0000 .CODE
.STARTUP
0017 B8 0040 MOV AX,40H ;set AX to 0040H
001A 8E D8 MOV DS,AX ;load data segment with 0040H
001C BE 0017 MOV SI,17H ;load SI with 0017H
001F 80 24 DF AND BYTE PTR [SI],0DFH ;clear NUM-LOCK bit
.EXIT
END
Assembly Language 3-35

Instruction Format
General Format of Instructions

Label: Opcode Operands ; Comment

Label: It is optional. It provides a symbolic address that can be used in branch instructions
Opcode: It specifies the type of instructions
Operands: Instructions of 80x86 family can have one, two, or zero operand
Comments: Only for programmers reference

Machine Code Format

Opcode Mode Operand1 Operand2

MOV AL, BL 1000100011000011


Register
MOV
mode
Assembly Language 3-36
Assembler Directives
Source File List File

0000 DATA SEGMENT PARA 'DATA


DATA SEGMENT PARA 'DATA
ORG 7000H
ORG 7000H
POINTS DB 16 DUP(?) 7000 0010 [00] POINTS DB 16 DUP(?)

SUM DB ? 7010 00 SUM DB ?


DATA ENDS 7011 DATA ENDS

CODE SEGMENT PARA 'CODE 0000 CODE SEGMENT PARA 'CODE'


ASSUME CS:CODE, DS:DATA ASSUME CS:CODE, DS:DATA
ORG 8000H
ORG 8000H
TOTAL: MOV AX,7000H
8000 B8 7000 TOTAL: MOV AX,7000H
MOV DS,AX
8003 8E D8 MOV DS,AX
MOV AL,0
8005 B0 00 MOV AL,0

CODE ENDS
END TOTAL

Assembly Language 3-37

Assembler Directives
SEGMENT directive
ENDS directive
END directive
ORG directive
DB: Define Byte; DW, .
ASSUME directive

Specifies the segment register (segment Register) that will be used to calculate the effective
addresses for all labels and variables defined under a given segment or group name (segment Na

If CS = 1230H and DS = 5678H, what are the physical memory addresses of


label TOTAL and variable SUM?

Assembly Language 3-38


Assembler Directives
Simplified Segment Directives Predefined .Mode Types
.MODEL SMALL
.DATA DATA CODE
SEGMENT SEGMENT
ORG 7000H
TINY one one
POINTS DB 16 DUP(?)
SMALL one one
SUM DB ?
MEDIUM one multiple

.CODE COMPACT multiple one

ORG 8000H LARGE multiple multiple

TOTAL: MOV AX,7000H HUGE multiple multiple


MOV DS,AX FLAT* one one
MOV AL,0

* Flat is used for 32-bit addressing

RET
END TOTAL
Assembly Language 3-39

Build Executable Programs

library
Assembler Linker
Source files
Syntax check OBJ
Translate source files
files into
machine code Executable
OBJ
files
files

Question: What is the difference between *.com and *.exe files?


http://www.faqs.org/faqs/msdos-programmer-faq/part2/section-9.html

Assemblers

Microsoft ML, LINK, & DEBUG


8086 Emulator
A86
MASM32 package

Assembly Language 3-40
Microsoft MASM and Debug
Microsoft MASM and Link Programs

Syntax check;
ML /c /Fl numoff.asm Translate assembly instructions into machine codes

Link numoff Build the executable file

Microsoft Debug Program


C:\> debug
-a
0BDF:0100 MOV AX, 40
0BDF:0103
-t
AX = 0040 BX = 0000 CX = 0000 DX = 0000 SP = .
..
-q

Assembly Language 3-41

8086/8088 Assembler and Emulator

Assembly Language 3-42


Difference Between EXE and Binary Files
Source File List File

.model small .model small


.data 0000 .data
org 0010H org 0200H
Var1 DB 12H 0200 12 Var1 DB 12H
.code 0000 .code
MOV AX, 0000 0000 B8 0000 MOV AX, 0000
MOV DS, AX 0003 8E D8 MOV DS, AX
label1: MOV AL, DS:[0100H] 0005 A0 0100 label1: MOV AL, DS:[0100H]
JMP label1 0008 EB FB JMP label1
end end

Assembly Language 3-43

Difference Between EXE and Binary Files


EXE file

Binary file

Assembly Language 3-44

S-ar putea să vă placă și