Chapter 4 (Part 2)
Download
Report
Transcript Chapter 4 (Part 2)
Overview of Assembly Language
Chapter 4
S. Dandamudi
Outline
• Assembly language
statements
• Data allocation
• Where are the operands?
• Overview of assembly language
instructions
Addressing modes
»
»
»
»
Register
Immediate
Direct
Indirect
• Defining constants
EQU, %assign, %define
• Data transfer instructions
mov, xchg, and xlat
Ambiguous moves
2005
Arithmetic
Conditional
Iteration
Logical
Shift/Rotate
• Macros
• Illustrative examples
• Performance: When to use the
xlat instruction
S. Dandamudi
Chapter 4: Page 2
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
Ambiguous moves: PTR directive
• For the following data definitions
.DATA
table1
status
TIMES
TIMES
20 DW
7 DB
0
1
the last two mov instructions are ambiguous
mov
mov
mov
mov
EBX, table1
ESI, status
[EBX],100
[ESI],100
Not clear whether the assembler should use byte or
word equivalent of 100
2005
S. Dandamudi
Chapter 4: Page 3
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
Ambiguous moves: PTR directive
• A type specifier can be used to clarify
• The last two mov instructions can be written as
mov
mov
WORD [EBX],100
BYTE [ESI],100
WORD and BYTE are called type specifiers
• We can also write these statements as
mov
mov
2005
[EBX], WORD 100
[ESI], BYTE 100
S. Dandamudi
Chapter 4: Page 4
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
Ambiguous moves: PTR directive
• We can use the following type specifiers:
Type specifier
Bytes addressed
BYTE
WORD
DWORD
QWORD
TWORD
2005
1
2
4
8
10
S. Dandamudi
Chapter 4: Page 5
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
The xchg instruction
• The syntax is
xchg
operand1,operand2
Exchanges the values of operand1 and operand2
Examples
xchg
xchg
xchg
EAX,EDX
[response],CL
[total],DX
• Without the xchg instruction, we need a
temporary register to exchange values using only
the mov instruction
2005
S. Dandamudi
Chapter 4: Page 6
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
The xchg instruction
• The xchg instruction is useful for conversion of
16-bit data between little endian and big endian
forms
Example:
xchg
AL,AH
converts the data in AX into the other endian form
• Pentium provides bswap instruction to do similar
conversion on 32-bit data
bswap
32-bit register
bswap works only on data located in a 32-bit register
2005
S. Dandamudi
Chapter 4: Page 7
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
Example
Assembly program to exchange values between AL
and BL.
.586
option segment:use16
.model small
.dosseg
.code
start:
mov al,5
; al = 5
mov bl,3
; bl = 3
xchg al,bl ; exchange values between al and bl
2005
S. Dandamudi
Chapter 4: Page 8
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
Example
mov dl,al
add dl,30h
mov ah,2h
int 21h
mov dl,0ah
mov ah,2h
int 21h
mov dl,0dh
int 21h
mov dl,bl
add dl,30h
2005
;print value of al
; Line feed to print new line
; carriage return character to print new line
; print value of bl
S. Dandamudi
Chapter 4: Page 9
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
Example
mov ah,2h
int 21h
mov ah,4ch
int 21h
end start
2005
; exit
S. Dandamudi
Chapter 4: Page 10
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
The xlat instruction
• The xlat instruction translates bytes
• The format is
xlatb
•
To use xlat instruction
» BX should be loaded with the starting address of the translation table
» AL must contain an index in to the table
– Index value starts at zero
» The instruction reads the byte at this index in the translation table and
stores this value in AL
– The index value in AL is lost
» Translation table can have at most 256 entries (due to AL)
2005
S. Dandamudi
Chapter 4: Page 11
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
The xlat instruction
Example: Encrypting digits
Input digits: 0 1 2 3 4 5 6 7 8 9
Encrypted digits: 4 6 9 5 0 3 1 8 7 2
The program:
.586
option segment:use16
.model small
Dosseg
.data
table db '4695031872'
.code
.startup
lea bx,table
2005
S. Dandamudi
Chapter 4: Page 12
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Data Transfer Instructions (cont’d)
The xlat instruction
mov ah,1h ;input character
int 21h
sub al,'0'
; converts input character to index
xlatb
; AL = encrypted digit character
mov dl,al
mov ah,2h ;print character located in table with input index
int 21h
mov ah,4ch
int 21h
end
2005
S. Dandamudi
Chapter 4: Page 13
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.