Chapter 4 Assembly Language and Programming
Chapter 4 Assembly Language and Programming
Assembler 汇编程序
Pseudo-instructions 伪指令 pseudo['su:dəʊ]
Directive 指示 ( 伪指令)
Instructions 指令(硬指令)
Debug [,di:‘bʌɡ] 调试
quadword[‘kwɔdwə:d] 四倍字长
Data Types 数据类型
Data Definition 数据定义
Case Sensitive 区分大小写
4.1 Introduction
Assembly language
An assembly language program is actually
a sequence of instructions.
Assembly language is a low level
language, which means it is machine
dependent.
• High level language such as C/C++ and
Java is machine independent.
04/21/20 4
Assembly language
• Direct access to computer hardware.
• Case insensitive
Operands
Operand type Example
Immediate value 96
Constant 2+4
expression
Register AX
Memory(variable) Variable name
• 0~3 operands
– 0: STC, CLC…
– 1: INC AX
– 2: MOV AX, BX
Comments
• Optional
• Start with semicolon (;)
• Explaining how the program works
Directives
• Embedded in the source code recognized
and acted upon by the assembler.
• Do not execute at run time.
• Do special programming actions during
the assembly process.
• Define variables, macros and procedures
Format
• [label] directive mnemonic [operand(s)]
[;comment]
Symbolic definition
• Associating a symbol with an integer
expression or some text.
• Equal-sign directive
– name=expression
– Example:
• Count=5
– Could be used for redefinition
• Count=5
• MOV AL, count
• Count =10
• ……
EQU directive
• Associates a symbolic name with an
expression or text.
– Name EQU expression
– Name EQU symbol
– Name EQU <text>
• Examples:
– Count EQU 100; count replaces 100
– SUM EQU 30+25; SUM replaces 30+25
– C EQU CX; C replaces register CX
• Can not be used for redefinition
Variable definition
• Define the size of variables, allocate
space for them, and assign them initial
values.
– Name DB expression ; define byte
– Name DW expression ; define word
– Name DD expression ; define double
word
– Name DQ expression ; define 8 bytes
– Name DT expression ;define 10 bytes
Examples
B1 DB 10H, 30H ; stores byte-size data10H, 30H
B2 DB 2x3+5 ; stores byte-size data 0BH
S1 DB ‘GOOD!’ ; byte-size ASCII code of ‘GOOD!’
W1 DW 1000H, 2030H ; word-size two data 1000H, 2030H
W2 DD 12345678H ; double-word size data 12345678H
S2 DB ‘AB’ ; byte-size ASCII code of ‘AB’
S3 DW ‘AB’ 41H,42H
; word-size ASCII code of ‘AB’
41H,42H
04/21/20
4.6.1 Fundamental Programming
Constructs
• Four basic programming construct
– Sequential construct
– Branch construct
– Cycle construct
– Subroutine call construct
the other
• No branches Operation 1
• No jumps
• No subroutine call
Operation 2
End
Example
Calculate the average value of two data x and y. The result should be
put into z. MOV BL,2
DATA SEGMENT DIV BL ; Q(AX/BL)->AL
x db 95D MOV z, AL
y db 87D RET
z db ? MAIN ENDP
DATA ENDS CODE ENDS
CODE SEGMENT END
MAIN PROC FAR
ASSUME CS:CODE, DS:DATA
START: MOV AL, x
ADD AL, y
MOV AH,0
ADC AH,0 ; put the carry
information
into AH
2. Branch construct
IF-THEN-ELSE SWITCH
Example
• Make a program to
do the function as Given the x, decide if
indicated: it’s negative or
positive or zero, then
give the value to y
1 x0 respectively.
y0 x0
1 x 0
DATA SEGMENT
x DB ?
y DB ?
DATA ENDS
CODE SEGMENT
MAIN PROC FAR
ASSUME CS:CODE, DS:DATA
START: MOV AL, x
CMP AL, 0 ; move data x into AL
JGE POS ; compare AL with 0
MOV y, -1D
RET
; if x>=0, jump to POS
POS: JE ZEO ;otherwise, assign -1 to y
MOV y, 1 ; if x=0, jump to ZEO
RET
ZEO: MOV y,0 ; otherwise, assign 1 to y
RET
MAIN ENDP
CODE ENDS
; assign 0 to y
END START
3. Cycle construct
First execute last judge First judge last execute
Example
• Question: Calculate the
number of negative
number in an array
• Assuming the first
element of array is the
number of the data, the
second element will be
used to store the result.
The data starts from the
third element of the array.
• Solution: Using one
register as counter of
negative numbers; one
register as counter of
data; DI as pointer to the
data in array.
DATA SEGMENT INC BL ;otherwise, inc BL
ARRAY DB 200 DUP(?) POSI: INC DI ; positive, change the
DATA ENDS pointer
CODE SEGMENT LOOP AGAIN;
ASSUME CS:CODE, DS:DATA MOV [SI+1], BL; move the
MAIN PROC FAR number to the second element of
START: LEA DI, ARRAY; DI point to ARRAY array
MOV SI, DI; SI point to ARRAY RET
MOV CL, [DI]; number of data into MAIN ENDP
CL CODE ENDS
XOR CH, CH; let CH=0 END START
MOV BL, 0; use BL as counter and
set 0
INC DI; change the pointer
INC DI; change the pointer
AGAIN: TEST BYTE PTR[DI], 80H; test the
SF
JZ POS ; if zero, jump to POS
4. Subroutine call construct
• Subroutine
– A dependent
procedure used for
lots of times.
• Call the subroutine
until the task has
been done and then
return to the
previous location of
the programming.
Example
Write a program to calculate the average value of the data
in the ARRAY. PADD PROC NEAR
PUSH AX
DATA SEGMENT PUSH CX
PUSH DX
ARRAY DW 30 DUP (?) PUSH SI ; push the data into stack to protect break
COUNT DW 30 point
LEA SI, ARRAY ; get the effect address of ARRAY
SUM DW 2 DUP (?) MOV CX, COUNT ; use CX as counter
DATA ENDS MOV DX, 0
AGAIN: ADD AX, [SI]; (AX)=(AX)+([SI])
CODE SEGMENT JNC NEXT
INC DX ; use DX to store carry information
MAIN PROC FAR NEXT: INC SI
ASSUME CS:CODE, DS:DATA INC SI ; go to next data
LOOP AGAIN
START: CALL PADD MOV SUM+2, DX
RET POP SI
POP DX
MAIN ENDP POP CX
POP AX
RET
PADD ENDP
CODE ENDS
END
4.6.3 Debug program
Bugs: errors in the program
Debug: the process of removing them
Two types:
Syntax error: No following the rules of coding
Execution error: Error in the logic behind the
development of the program.
“Debug ” is an environment for one to
learn and debug assembly language
programs.
Content of internal register
State of CPU
4.6.4 Interrupt Calls
• Two types
– Hardware interrupt
– Software interrupt
• Interrupt vectors table
– Pointed by interrupts
– Address of interrupt entry (CS:IP)
Interrupt instructions
• Three instructions for programmer:
– INT
• Software interrupt instruction
– INTO
• Overflow interrupt
– INT 3
• Special software interrupt as interrupt
instruction is 1 byte
INT N
• 0<=N<=256, N*4=vector table address
• Steps:
– Pushes the flags onto the stack
– Clear trace flag and interrupt-enable flag
– Push the current CS register onto the stack
– Fetches the new CS location from the vector
table
– Push the current IP onto the stack
– Fetches the new IP location from the vector table
– Jump to this new location
INTO
• Encountered when overflow happens
(OF=1)
• Vector table address is 0010H
• An INTO appears after every addition and
subtraction
– OF=0, no operation executed.
– OF=1, go interrupt.
INT 3
• Breakpoint interrupt.
• Interrupt instruction is 1 byte.
• Often used for debug.
– Display the contents of all register
IRET
• Interrupt return instruction
– Pop stack data back into IP
– Pop stack data back into CS
– Pop stack data back into flags.
• Resume the program interrupted
before.
Dos Function Calls
• Control the IBM-PC and its compatible
system
– Reading and writing data to the disk and
managing the keyboard and displays.
• Format
– MOV AH, call number (Table 4.10)
– INT 21H or INT 10H
INT 21H DOS function calls
INT 10H BIOS function calls
4.7 SAMPLES AND EXAMPLES
4.7.1 Computational Routines
1. Binary addition
– Ex: Calculate the sum of two string data
DATA SEGMENT
M1 DB 20 DUP (?)
M2 DB 20 DUP(?)
M3 DW 20 DUP(0)
DATA ENDS
CODE SEGMENTS
ASSUME CS:CODE, DS:DATA
LEA SI, M1 ; get the effect address of M1,M2 and M3
LEA DI, M2
LEA BX, M3
MOV CX, 20 ; use CX as counter
AA1:MOV AL, [SI] ; move data of array 1 into AL
; add the data of array 2 with array 1
ADD AL, [DI]
; move the result into array 3
MOV [BX], AL
; save the carry information in the higher-byte of
ADC BYTE PTR[BX+1],0 array 3
INC SI ; point to next data of array 1
INC DI ; point to next data of array 2
ADD BX, 2 ; point the next data location of array 3
LOOP AA1 ; do the addition again
MOV AX, 4C00H ; move the function number of interrupt into AH
INT 21H ; finish
CODE ENDS
END START
2. Binary subtraction
WAVE1 DW 2048 DUP(?)
WAVE2 DW 2048 DUP(?)
……
SUBWAVE PRCO FAR
LEA SI, WAVE1 ; init pointer to beginning of WAVE1
LEA DI, WAVE2 ; init pointer for WAVE2
MOV CX, 2048 ; init loop counter
SUBEM: MOV AX, [SI] ;get data from WAVE1
SUB [DI], AX ; subtract and replace WAVE2 data
ADD SI, 2 ; advance WAVE1 pointer
ADD DI,2 ; advance WAVE2 pointer
LOOP SUBEM ; do all the subtraction
RET
SUBWAVE ENDP
3. BCD addition
• BCD binary
– 1 digit using 4 bits
– 1 byte storage: 0~99
– 2 byte storage: 0~9999
• Eliminate the round-off error
– 0.7D=0.101100110011…
– 0.10110011…=0.69999…
• BCD adjust:
– 34298: 00 03 43 98H
– 7571364: 07 57 13 64H
DAA: Decimal adjust after addition
• Adjust result into packed BCD code after
BCD addition
– If lower 4 bit of AL >=9 or AF=1, AL=AL+06H
and set AF=1
– If high 4 bit of AL>=9 or CF=1, AL=AL+60H
and set CF=1.
– If neither, Clear CF and AF.
• Two BCD number addition, NUMA and NUMB
NUMA DB 4 DP (?)
NUMB DB 4 DP(?)
ADDBCD PRCO FAR
MOV SI,3 ; init pointer to LSB
MOV CX,4 ; init loop counter
CLC ; clear carry to start
DECIADD: MOV AL, NUMA [SI] ; get first BCD number
ADC AL, NUMB[SI] ; add second BCD number
DAA ; correct result into BCD
MOV NUMB [SI], AL ; save result
DEC SI ; point to next pair to digits
LOOP DECIADD ; do all the addition
RET
ADDBCD ENDP
4. BCD subtraction
NUMA DB 4 DUP(?)
NUMB DB 4 DUP(?)