Download Chapter 4 (Part 3)

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
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.
Overview of Assembly Instructions
• Pentium provides several types of instructions
• Brief overview of some basic instructions:






Arithmetic instructions
Jump instructions
Loop instruction
Logical instructions
Shift instructions
Rotate instructions
• These sample instructions allows you to write
reasonable assembly language programs
2005
 S. Dandamudi
Chapter 4: Page 3
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Overview of Assembly Instructions (cont’d)
Arithmetic Instructions
INC and DEC instructions
 Format:
inc
destination
dec
destination
 Semantics:
destination = destination +/- 1
» destination can be 8-, 16-, or 32-bit operand, in memory
or register
No immediate operand
• Examples
inc
dec
2005
BX
[value]
; BX = BX+1
; value = value-1
 S. Dandamudi
Chapter 4: Page 4
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Overview of Assembly Instructions (cont’d)
Arithmetic Instructions
ADD instruction
 Format:
add
destination,source
 Semantics:
destination = (destination)+(source)
• Examples
add
add
 inc
BX, AX
[value],10H
AX
is better than
add AX,1
– inc takes less space
– Both execute at about the same speed
2005
 S. Dandamudi
Chapter 4: Page 5
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Overview of Assembly Instructions (cont’d)
Arithmetic Instructions
SUB instruction
 Format:
sub
destination,source
 Semantics:
destination := (destination)-(source)
• Examples
sub
sub
 dec
BX, AX
[value],10H
AX
is better than
sub AX,1
– dec takes less space
– Both execute at about the same speed
2005
 S. Dandamudi
Chapter 4: Page 6
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Overview of Assembly Instructions (cont’d)
Arithmetic Instructions
CMP instruction
 Format:
cmp
destination,source
 Semantics:
(destination)-(source)
 destination and source are not altered
 Useful to test relationship (>, =) between two operands
 Used in conjunction with conditional jump instructions
for decision making purposes
• Examples
cmp
2005
BX, AX
cmp
 S. Dandamudi
[count],100
Chapter 4: Page 7
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Overview of Assembly Instructions (cont’d)
Jump Instructions
Unconditional Jump
 Format:
jmp
label
 Semantics:
» Execution is transferred to the instruction identified by label
• Examples: Infinite loop
mov
inc_again:
inc
jmp
mov
2005
AX,1
AX
inc_again
BX, AX
; never executes this
 S. Dandamudi
Chapter 4: Page 8
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Overview of Assembly Instructions (cont’d)
Jump Instructions
Conditional Jump
 Format:
j<cond>
label
 Semantics:
» Execution is transferred to the instruction identified by label
only if <cond> is met
• Examples: Testing for carriage return
2005
mov ah,1h
int 21h
cmp
AL,0DH ; 0DH = ASCII carriage return
je
CR_received
inc
CL
...
CR_received:
 S. Dandamudi
Chapter 4: Page 9
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Overview of Assembly Instructions (cont’d)
Jump Instructions
Conditional Jump
 Some conditional jump instructions
– Treats operands of the CMP instruction as signed numbers
je
jg
jl
jge
jle
jne
2005
jump
jump
jump
jump
jump
jump
if
if
if
if
if
if
equal
greater
less
greater or equal
less or equal
not equal
 S. Dandamudi
Chapter 4: Page 10
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Overview of Assembly Instructions (cont’d)
Jump Instructions
Conditional Jump
 Conditional jump instructions can also test values of the
individual flags
jz
jnz
jc
jnc
jump
jump
jump
jump
if
if
if
if
zero (i.e., if ZF = 1)
not zero (i.e., if ZF = 0)
carry (i.e., if CF = 1)
not carry (i.e., if CF = 0)
 jz is synonymous for je
 jnz is synonymous for jne
2005
 S. Dandamudi
Chapter 4: Page 11
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Overview of Assembly Instructions (cont’d)
Loop Instruction
LOOP Instruction
 Format:
loop
target
 Semantics:
» Decrements CX and jumps to target if CX  0
– CX should be loaded with a loop count value
• Example: Executes loop body 50 times
mov
CX,50
repeat:
<loop body>
loop
repeat
...
2005
 S. Dandamudi
Chapter 4: Page 12
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Overview of Assembly Instructions (cont’d)
Loop Instruction
• The previous example is equivalent to
mov
CX,50
repeat:
<loop body>
dec
CX
jnz
repeat
...
 Surprisingly,
dec
jnz
CX
repeat
executes faster than
loop
2005
repeat
 S. Dandamudi
Chapter 4: Page 13
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Assembly Programs (Example1)
;Assembly program to add 5 numbers and
;print the result on screen
.586
DOSSEG
OPTION SEGMENT: USE16
.MODEL SMALL
.DATA
numbers db 1,2,3,1,2
.CODE
START:
.startup
mov bx,offset numbers
mov cx,5
;initialize counter
2005
 S. Dandamudi
Chapter 4: Page 14
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Assembly Programs (Example1)
mov
target:add
inc
dec
jnz
add
mov
int
mov
int
END
2005
dl,0
dl,[bx]
bx
cx
target
; jump if cx<>0
dl,'0'
ah,2h
;print the sum
21h
AX, 4C00h
21h
 S. Dandamudi
Chapter 4: Page 15
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Assembly Programs (Example2)
;Assembly program to continuous reading a
;character and print the next until
;read ‘*’.
.586
DOSSEG
OPTION SEGMENT: USE16
.MODEL SMALL
.CODE
START: .startup
target:mov ah,1
;read a character
int 21h
cmp al,'*'
je exit ;jump to exit if al==‘*’
2005
 S. Dandamudi
Chapter 4: Page 16
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Assembly Programs (Example2)
inc
mov
mov
int
mov
mov
int
mov
int
jmp
exit:mov
int
al
;find next character
dl,al
ah,2
;print next char
21h
dl,0dh ;print new line
ah,2
21h
dl,0ah
21h
target
;continuous loop
AX, 4C00h
21h
END
2005
 S. Dandamudi
Chapter 4: Page 17
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Assembly Programs (Example3)
;Assembly program to read numbers and
;print the sum if it is <=9
.586
DOSSEG
OPTION SEGMENT: USE16
.MODEL SMALL
.DATA
msg db 'Enter numbers:$',0dh,0ah
res db 0dh,0ah,'Sum =$'
sum db 0
.CODE
START: .startup
mov dx,offset msg
2005
 S. Dandamudi
Chapter 4: Page 18
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Assembly Programs (Example3)
mov ah,9h
;print msg text
int 21h
startloop:mov ah,1
;read a number
int 21h
sub al,'0' ; convert to hex
add sum,al ;add number to sum
cmp sum,9
;compare sum with 9
jl startloop;if sum<9 jump to
;startloop label
sub sum,al ;sub last number
;from sum
add sum,'0';convert to dec.
mov dx,offset res
2005
 S. Dandamudi
Chapter 4: Page 19
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Assembly Programs (Example3)
mov
int
mov
mov
int
mov
int
ah,9
;print text of res
21h
dl,sum
ah,2
;print sum
21h
AX, 4C00h
21h
END
2005
 S. Dandamudi
Chapter 4: Page 20
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.