Download Understanding Assembly Language Syntax And Addressing Modes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
EE2801 -- Lecture 8
Understanding Assembly
Language Syntax And
Addressing Modes
EE2801-L08P01
The Basics Of Assembly Language Syntax
We’ve already seen some examples of assembly code, and hopefully they have been fairly
readable. Now it’s time to be a little more formal. The basic syntax of a line of assembly
code is:
label:
instruction
[operands]
;comment(s)
For example:
add1:
inc ax ;Increment the value in the ax register
In this syntax:
·
the label, is converted into an address by the compiler. This label gives us a handy
name for referring to a part of our code.
·
the instruction is also converted into a number by the compiler. In this case, that
number is actually executed by the machine.
·
the operands are the parameters that the instruction needs to execute. A given
instruction may or may not require operands.
·
the comment(s) convey useful information about exactly what a line of code is doing
(and why). Good comments are essential in assembly language programming. In
fact, I often
start by writing the comments.
EE2801-L08P02
The Anatomy Of An Assembly Language Program
In addition to the basic syntax for lines of code, there are several lines in a typical program
that are instructions not to the computer, but rather to the compiler. Symbols such as
.Model, .Stack, .Data, .Code, .8086, equ, db, END, and so on are used to tell the compiler
what you would like to do.
For example:
; Simple program to demonstrate the anatomy of an assembly language program.
;
.Model
small ;64K maximum memory size.
.Stack
100h ;Reserve 256 bytes for the stack.
MaxIndex
Count
.Data
equ 10d
db
?
;Mark the start of the data space.
;Maximum iterations through the loop.
;Current iteration count;
.Code
.8086
;Mark start of code segment.
;Force the generation of 8086 compatible code.
; Main Program: Count through all the indicies and stop.
start:
mov ax,@data
;Move the data segment address into the AX register.
mov ds,ax
;Load the data segment address into the segment register.
Iterate:
exit:
END
mov
xor
mov
cmp
je
inc
mov
mov
jmp
bl,MaxIndex
al,al
[Count],al
al,bl
exit
al
[Count],al
al,[Count]
Iterate
jmp exit
start
;Load the maximum count into AX.
;Clear the AL register.
;Initialize the count variable to zero.
;Is the count and MaxIndex the same?
; If yes, then exit.
;Increment the count.
;Save the count in memory.
;Get the count from memory.
;Iterate until finished.
;Loop forever.
;Compiler directive indicating the end of the program.
EE2801-L08P03
Registers in the x86 Microprocessor
There are really two memory spaces that a microprocessor works in. One is the “big”
memory space we normally think about when we talk about computers. This “big” space
may contain millions of bytes and is where programs and data are stored during execution.
The other memory space of the microprocessor is a “tiny” space consisting of the internal
processor registers. This space usually consists of a dozen or so bytes that are used for
temporary storage.
Depending on the instruction being executed, a register may be “general purpose,”
meaning that it can contain anything, or it may have restrictions on its use (for example,
there is a specific relationship between the loop instruction and the CX register).
Almost every assembly language instruction uses registers or memory addresses in some
way. The specific meaning of an operation, therefore, is dependent on the details of how
the instruction uses its registers or memory.
These details determine the “addressing mode” of the instruction.
EE2801-L08P04
Register Addressing Mode
In the 80x86 there are 8 working registers that are 16 bits wide. Four of these registers,
AX, BX, CX, and DX can be thought of as either one 16-bit register or as two 8-bit registers.
Working Register
AX
BX
CX
DX
BP
DI
SI
SP
High Byte
AH
BH
CH
DH
Low Byte
AL
BL
CL
DL
Register addressing uses these register names to refer to the data held in the register.
Note that operations must be the same size!
mov
mov
mov
mov
ax,
bl,
ax,
al,
cx
cl
sp
si
;Move 16 bits from cx into ax.
;Move 8 bits from cl into bl.
;Move the stack pointer into ax.
;ILLEGAL! Operands are not the same size!
EE2801-L08P05
Immediate Addressing
Most instructions assume that the operands of an instruction are actually the address of the
data that is involved -- not the data itself. The exception to this rule is the immediate
addressing mode.
In the immediate addressing mode the actual data to be transferred is one of the operands
of the instruction. For example:
item1
item2
item3
equ
equ
equ
0F3
27d
8001
start:
mov ax, item1
mov bl, 87d
mov ch, item3
mov si, item3
mov item2,al
;The label “item1” equals F3 hex.
;The label “item2” equals 27 decimal.
;The label “item3” equals 8001 hex.
;AX will be set
;BL will be set
;ILLEGAL! item3
;SI will be set
;ILLEGAL! item2
to
to
is
to
is
00F3.
87 decimal.
too large to fit in 8 bits!
8001 hex.
just a number, NOT a memory location!
EE2801-L08P06
Direct Memory Addressing
Accessing data that is in memory is done through the address of the data item in memory.
This addressing mode is invoked when the operand is placed in square brackets. What
these square brackets mean is that the item enclosed in the brackets is really an offset into
a memory segment. In most cases, this will be an offset into the data segment.
item1
item2
var1
var2
equ
equ
dw
db
0F3
8001
?
?
;The label “item1” equals F3 hex.
;The label “item3” equals 8001 hex.
;var1 is the address of the first of two bytes in memory.
;var2 is the address of a byte in memory.
start:
mov ax, item1 ;AX will be set to 00F3.
mov [var1],ax ;The value 00F3 will be written to var1 and var1+1.
mov cx,[var1] ;CX = 00F3;
mov dh,[byte 10F8h];DH contains the 8-bit value at address DS:10f8.
mov [byte A37Eh],dl;DL hex will be written to memory location DS:A37E!
EE2801-L08P07
Indirect Memory Addressing
Indirect addressing means that one address acts as a pointer to the real address of
interest. For example, suppose we wanted to fill a large number of memory locations with
a single byte of data. Using direct addressing, we’d have to have one line of code for each
byte moved. Using indirect addressing, we can use a register to hold an address and make
our coding much simpler:
mem
equ
6000h
;Starting address in memory
mov
mov
mov
mov
ax,mem
ds,ax
al,0aah
bx,2000h
;AX = 6000h
;The Data Segment points to the starting address.
;Aah is the byte to write to memory.
;We want to move a total of 2000h bytes (8k).
more:
dec
mov
jnz
bx
[bx],al
more
;Decrement the byte counter.
;Write aa hex to DS:BX (61FFF on the 1st pass).
;Loop until bx = 0 (DS:BX = 60000).
stop:
jmp
stop
;Hang the system.
EE2801-L08P08