The basic elements of an assembler program

advertisement
The basic elements of an assembler
program
Statements – one per line.
The format of a line:
[identifier] instruction/directive [operands] [; comment]
Identifier
• name of a variable
• label (including the name of a procedure)
• name of a constant
• name of a segment
Make your labels meaningful!
Find out if register AL contains a code for a lower case letter.
If yes, convert it to the corresponding upper case letter.
cmp al,’a’
jb
L1
cmp al,’z’
ja L1
sub al,20h ; convert to upper case
L1: …
Assembly Language 4/ 1
Better:
cmp al,’a’
jb NotALowerCaseLetter
cmp al,’z’
ja NotALowerCaseLetter
sub al,20h
NotALowerCaseLetter:
…
Directives
They are not compiled to a machine code, but they may:
define constants, variables, labels, segments, procedures
and macros
enable compilation of instructions of the 80186, 80286,
80386, 80486, Pentium processors and the 80287 and
80387 coprocessors
control the listing contents and format
control the conditional compilation (e.g. directives IF,
ENDIF, ELSE)
Assembly Language 4/ 2
Operands
Possible operands:
registers
addresses
constant values
An instruction may have:
none operand
1 operand
2 operands
o the right one: a source
o the left one:
a source
cmp al,0
a destination
mov al,0
a source and a destination
add al,bl; al := al+bl
The left operand cannot be a constant value.
If the operands are registers or memory locations, they
must be of the same types.
Two memory operands are not allowed in the
instruction!
3 operands
imul ax,bx,5; ax := bx*5
o the leftmost one: a destination
o the middle and rightmost ones: sources
Assembly Language 4/ 3
Variables
- symbolic addresses of data items (offsets in the data
segment)
- defined by directives DB, DW, DD, DF, DP, DQ, DT.
Syntax:
[name of the variable] Dx expression [,expression] ...
Directive Dx:
determines the variable type (according to the letter x)
allocates the space in memory (one or more bytes)
initialises the contents of the memory locations (does not
initialise, if the expression is ?)
Assembly Language 4/ 4
Directive Allocated
memory
size in
bytes
DB
1
Variab Variable may contain
le type
byte
Signed integer in the range
<-128; 127>
Unsigned integer in the range
<0; 255>
Character
DW
2
word
Signed integer in the range
<-32 768; 32 767>
Unsigned integer in the range
<0; 65 535>
16-bit offset
DD
4
dword Signed integer
Unsigned integer
Single precision floating point
number in the range about
±1038
Far pointer in 16-bit mode, i.e.
address in the segment:offset
form
32-bit offset
Assembly Language 4/ 5
DP
6
pword Signed integer
fword Unsigned integer
DF
Far pointer in 32-bit mode, i.e.
address in the segment:offset
form
DQ
8
qword Signed integer
Unsigned integer
Double precision floating point
number in the range about
±10308
DT
10
tbyte
Signed integer
Unsigned integer
Packed BCD number
Extended precision floating
point number in the range
about ±104932
Assembly Language 4/ 6
.DATA
ORG 100h
; contents of memory locations
beginning at offset 100h:
One DB –1,12+1; FF 0D
DB ’abcd’
; 61 62 63 64
DB 3 dup (?)
; ?? ?? ??
DW -32768
; 00 80
DW One
; 00 01
DD 0.1
; CC CC CC 3D
DD 0ABCDEF23h ; 23 EF CD AB
DD One
; 00 01 xy uv
Multi-byte data are
stored in the reverse
order of bytes.
0100 is the offset
of variable One
uvxy is the base
address of the
data segment
DT 1234567890 ; 90 78 56 34 12 00 00 00 00 00
DT -1234567890; 90 78 56 34 12 00 00 00 00 80
Assembly Language 4/ 7
Labels
- symbolic addresses of instructions
- type: near a far
Type near
The label is the offset of an instruction in the code segment.
A jump to the label is in the same code segment.
Loop: mov ax,bx
Type far
The label is the complete address of an instruction
(segment:offset).
A jump to the label is in another code segment.
DoIt LABEL far
mov ax,bx
Attributes of variables and labels
segment – the base address of the segment where the
variable/label is defined
offset
type
Assembly Language 4/ 8
Value-returning operators
Operator
Use
seg
Return the value of attribute segment.
offset
Return the value of attribute offset.
type
Return the
value
according to the variable/label
type
1
byte
2
word
4
dword
6
fword/pword
8
qword
10
tbyte
-1
near
-2
far
length
Return the number of data items allocated to
the variable.
size
Return the number of bytes in the variable, i.e.
the value length*type.
Assembly Language 4/ 9
Value DW 1234h
Test_data DB 5,6,7
Array DW 100 dup(0)
mov al,type Value; al := 2
mov bl,type Test_data; bl := 1
mov cl,type Array; cl := 2
mov ah,length Value; ah := 1
mov bh,length Test_data; bh := 1
mov ch,length Array; ch := 100
mov dx,size Array; dx := 200
length returns value > 1 only if the variable has been
defined using operator dup
Assembly Language 4/ 10
Standard and alternative segment registers
Memory reference
Standard
segment
register
Alternative
segment
register
Label
CS
-
Direct address
DS
CS, ES, FS,
GS, SS
SS
CS, DS, ES,
FS, GS
Source string
DS
CS, ES, FS,
GS, SS
Destination string
ES
-
Indirect address with base BX
and/or index SI, DI
Indirect address with base EAX,
EBX, ECX, EDX, ESI, EDI
Indirect address with base BP
Indirect address with base EBP,
ESP
Assembly Language 4/ 11
Operators, which override attribute values
Operator
Override attribute
:
segment
ptr
typ
mov al,ds:[bp]
Value DW 1234h
Number DB 5,6
mov ax,Value; al := 34h, ah := 12h
Value:
34h
AX
12h
34h
12h
Number:
5
6
Assembly Language 4/ 12
mov ah,byte ptr Value; ah := 34h
mov bx,word ptr Number; bx := 605h
The same as
mov bl,Cislo
mov bh,Cislo+1
AH
Value:
34h
34h
12h
Number:
BX
5
6
06h
05h
byte?
word?
mov [bx],1
byte!
mov byte ptr [bx],1
mov word ptr [bx],1
word!
Similarly: inc [bx],1
Assembly Language 4/ 13
Constants
- make the orientation in the program and its modification
more easier
- defined by directives EQU
The $ predefined symbol is equal to the current offset in the
segment.
Example
Display the string “Ahoj!” stored in variable String.
String:
‘A’
String + 1:
‚h‘
String + 2:
‚o’
String + 3:
‚j‘
String + 4:
‚!‘
StringLength
$:
Assembly Language 4/ 14
.MODEL small
.STACK 100h
.DATA
String DB ’Ahoj!’
StringLength EQU $-String
.CODE
Start:mov ax,@data
mov ds,ax; point ds to the data segment
mov di,0; the 1st character has index 0
mov bx,offset String; point bx to the
storage location for the first character
mov cx,StringLength
DisplayChar:
mov dl,[bx+di]; copy a character at
address ds:bx+di to dl
mov ah,2
int 21h; display the character whose
ASCII code is in dl
inc di; increment index by 1 – go to
the next character
loop DisplayChar; cx := cx – 1,
if cx > 0, jump to DisplayChar
Stop: mov ax,4C00h
int 21h
END Start
Assembly Language 4/ 15
Download