5-Assembly Programming
5-Assembly Programming
Microprocessor
Mohammad Hosseini
Assembly Programming
2
Assembly Language Programs
❖ A series of statements (lines)
➢ Directives (pseudo-instructions)
▪ Give directions to the assembler about how it should translate the
Assembly language instructions into machine code
1
3/2/2024
3
Instruction Format
❖ The label field allows the program to refer to a line of code by name
✓ Must end with a colon when it refers to an instruction
✓ Labels for directives do not need to end with a colon
❖ Mnemonic and the operands perform the real work of the program.
4
Directives
2
3/2/2024
5
Example of an Assembly Program
;An assembly language program using simplified segment definition
❖ Simple segment definition .MODEL SMALL
.STACK 64
using models .DATA
DATA1 DB 52H
❖ Full segment definition DATA2 DB 29H
SUM DB ?
(later) .CODE
MAIN PROC FAR ;this is the program entry point
MOV AX,@DATA ;load the data segment address
MOV DS,AX ;assign value to DS
MOV AL,DATA1 ;get the first operand
MOV BL,DATA2 ;get the second operand
ADD AL,BL ;add the operands
MOV SUM,AL ;store the result in location SUM
MOV AH,4CH ;set up to return to DOS
INT 21H
MAIN ENDP
END MAIN ;this is the program exit point
6
Model Definition
❖ The MODEL directive ;An assembly language program using simplified segment definition
.MODEL SMALL
selects the size of the .STACK 64
memory model .DATA
➢ SMALL: code ≤ 64 KB DATA1 DB 52H
data ≤ 64 KB DATA2 DB 29H
➢ MEDIUM: code ≥ 64 KB SUM DB ?
data ≤ 64 KB .CODE
➢ COMPACT: code ≤ 64 KB MAIN PROC FAR ;this is the program entry point
data ≥ 64 KB MOV AX,@DATA ;load the data segment address
➢ LARGE: code ≥ 64 KB MOV DS,AX ;assign value to DS
data ≥ 64 KB MOV AL,DATA1 ;get the first operand
▪ no single set of data should MOV BL,DATA2 ;get the second operand
exceed 64K ADD AL,BL ;add the operands
➢ HUGE: code ≥ 64 KB MOV SUM,AL ;store the result in location SUM
data ≥ 64 KB MOV AH,4CH ;set up to return to DOS
▪ data items (such as arrays) can INT 21H
exceed 64K MAIN ENDP
➢ TINY: code+data ≤ 64 KB END MAIN ;this is the program exit point
3
3/2/2024
7
Segment Definition
❖ Every line of a program ;An assembly language program using simplified segment definition
.MODEL SMALL
must correspond to one of .STACK 64
the 8086 segments (Code, .DATA
Data, Stack, Extra) DATA1 DB 52H
DATA2 DB 29H
SUM DB ?
❖ The simplified segment .CODE
definition format uses three MAIN PROC FAR ;this is the program entry point
simple directives: MOV AX,@DATA ;load the data segment address
MOV DS,AX ;assign value to DS
➢ .CODE MOV AL,DATA1 ;get the first operand
➢ .DATA MOV BL,DATA2 ;get the second operand
➢ .STACK ADD AL,BL ;add the operands
▪ Only three segments can MOV SUM,AL ;store the result in location SUM
be defined MOV AH,4CH ;set up to return to DOS
INT 21H
▪ Automatically correspond ENDP
MAIN
to the CPU’s CS, DS, SS END MAIN ;this is the program exit point
8
Stack Segment
;An assembly language program using simplified segment definition
❖ Reserves 64 bytes of .MODEL SMALL
.STACK 64
memory for the stack .DATA
DATA1 DB 52H
DATA2 DB 29H
SUM DB ?
.CODE
MAIN PROC FAR ;this is the program entry point
MOV AX,@DATA ;load the data segment address
MOV DS,AX ;assign value to DS
MOV AL,DATA1 ;get the first operand
MOV BL,DATA2 ;get the second operand
ADD AL,BL ;add the operands
MOV SUM,AL ;store the result in location SUM
MOV AH,4CH ;set up to return to DOS
INT 21H
MAIN ENDP
END MAIN ;this is the program exit point
4
3/2/2024
9
Data Segment
❖ DB directive ;An assembly language program using simplified segment definition
.MODEL SMALL
➢ (Define Byte) .STACK 64
.DATA
➢ It is used by the assembler to DATA1 DB 52H
allocate memory in byte-sized DATA2 DB 29H
SUM DB ?
chunks.
.CODE
➢ The data items defined in the MAIN PROC FAR ;this is the program entry point
data segment will be accessed MOV AX,@DATA ;load the data segment address
in the code segment by their MOV DS,AX ;assign value to DS
labels. MOV AL,DATA1 ;get the first operand
MOV BL,DATA2 ;get the second operand
ADD AL,BL ;add the operands
➢ DATA1 and DATA2 are given MOV SUM,AL ;store the result in location SUM
initial values. MOV AH,4CH ;set up to return to DOS
➢ SUM is not given an initial INT 21H
value, but storage is set aside MAIN ENDP
END MAIN ;this is the program exit point
for it.
10
Code Segment
❖ Program instructions ;An assembly language program using simplified segment definition
.MODEL SMALL
.STACK 64
❖ PROC directive .DATA
DATA1 DB 52H
➢ A procedure is a group of DATA2 DB 29H
instructions designed to SUM DB ?
accomplish a specific function .CODE
MAIN PROC FAR ;this is the program entry point
➢ Procedure definition: MOV AX,@DATA ;load the data segment address
label PROC [FAR | NEAR] MOV DS,AX ;assign value to DS
MOV AL,DATA1 ;get the first operand
[statements]
MOV BL,DATA2 ;get the second operand
label ENDP ADD AL,BL ;add the operands
➢ Entrance proc should be FAR MOV SUM,AL ;store the result in location SUM
MOV AH,4CH ;set up to return to DOS
INT 21H
MAIN ENDP
END MAIN ;this is the program exit point
5
3/2/2024
11
Code Segment
;An assembly language program using simplified segment definition
➢ On program start, the OS .MODEL SMALL
assigns CS and SS. .STACK 64
.DATA
DATA1 DB 52H
➢ The DS value must be DATA2 DB 29H
initialized by the program. SUM DB ?
.CODE
MAIN PROC FAR ;this is the program entry point
✓ DATA refers to the start MOV AX,@DATA ;load the data segment address
of the data segment MOV DS,AX ;assign value to DS
MOV AL,DATA1 ;get the first operand
MOV BL,DATA2 ;get the second operand
ADD AL,BL ;add the operands
MOV SUM,AL ;store the result in location SUM
MOV AH,4CH ;set up to return to DOS
INT 21H
MAIN ENDP
END MAIN ;this is the program exit point
12
Code Segment
;An assembly language program using simplified segment definition
.MODEL SMALL
➢ Program .STACK 64
.DATA
DATA1 DB 52H
DATA2 DB 29H
SUM DB ?
.CODE
MAIN PROC FAR ;this is the program entry point
MOV AX,@DATA ;load the data segment address
MOV DS,AX ;assign value to DS
MOV AL,DATA1 ;get the first operand
MOV BL,DATA2 ;get the second operand
ADD AL,BL ;add the operands
MOV SUM,AL ;store the result in location SUM
MOV AH,4CH ;set up to return to DOS
INT 21H
MAIN ENDP
END MAIN ;this is the program exit point
6
3/2/2024
13
Code Segment
;An assembly language program using simplified segment definition
.MODEL SMALL
.STACK 64
.DATA
DATA1 DB 52H
DATA2 DB 29H
SUM DB ?
.CODE
➢ Return control to the MAIN PROC FAR ;this is the program entry point
operating system MOV AX,@DATA ;load the data segment address
MOV DS,AX ;assign value to DS
MOV AL,DATA1 ;get the first operand
MOV BL,DATA2 ;get the second operand
ADD AL,BL ;add the operands
MOV SUM,AL ;store the result in location SUM
➢ The END pseudo-instruction MOV AH,4CH ;set up to return to DOS
ends the entire program by INT 21H
indicating to DOS that the MAIN ENDP
entry point MAIN has ended. END MAIN ;this is the program exit point
14
Sample Shell of an Assembly Program
;An assembly language program using simplified segment definition
.MODEL SMALL
.STACK 64
.DATA
;
;place data definitions here
;
.CODE
MAIN PROC FAR ;this is the program entry point
MOV AX,@DATA ;load the data segment address
MOV DS,AX ;assign value to DS
;
;place code here
;
MOV AH,4CH ;set up to return to DOS
INT 21H ;return to DOS
MAIN ENDP
END MAIN ;this is the program exit point
7
3/2/2024
15
Full Segment Definition
➢ A program can have several
segments of each type
16
Build Procedure
myfile.asm
➢ .obj: object file (machine language) created
by assembler
Assembler
➢ .lst: lists opcodes, offset addresses and myfile.lst
detected errors other obj files
myfile.obj
8
3/2/2024
17
Example of lst and map
18
Tools
9
3/2/2024
19
Control Transfer Instructions
Ranges:
❖ SHORT
➢ Intrasegment (if control is transferred to a memory
location within the current code segment)
➢ Only IP changes
Instructions:
➢ One-byte range (within -128 to + 127 bytes of the
current value of IP)
❖Jumps ❖ NEAR
➢ Intrasegment (control is transferred to a memory
❖CALL location within the current code segment)
➢ Only IP changes
➢ Two-bytes range (±32K bytes)
❖ FAR
➢ Intersegment (if control is transferred outside the
current code segment)
➢ CS and IP change
20
Conditional Jumps
❖ Jump according to the value of the
flag register 1067:0005 890500 MOV CX,05
1067:0008 880000 MOV BX,OFFSET DATA_IN
1067:000D 0207 AGAIN: ADD AL,[BX]
❖ All conditional jumps are short jumps 1067:000F 43 INC BX
1067:0010 49 DEC CX
1067:0011 75FA JNZ AGAIN
❖ The conditional jump is a two-byte 1067:0013 A20500 MOV SUM,AL
instruction:
➢ one byte is the opcode of the J condition
➢ the second byte is a value between 00
and FF
✓ The second byte is added to the IP
of the instruction after the jump
10
3/2/2024
21
Conditional Jumps
22
Unconditional Jump
11
3/2/2024
23
Unconditional Jump
❖ SHORT JMP SHORT label
▪ The operand is 1 byte
✓ More efficient code
❖ NEAR (The default form)
➢ Direct jump JMP label
▪ The target address can be anywhere in the segment within the
range -32768 to +32767 of the current IP
➢ Register indirect jump JMP BX
▪ The target address is in a register. IP takes the register’s value
➢ Memory indirect jump JMP [DI]
▪ The target address is the contents of two memory locations
pointed at by the register
❖ FAR JMP FAR PTR label
▪ A jump out of the current code segment
▪ Both CS and IP change
24
Subroutines and CALL Statement
12
3/2/2024
25
Shell of Subroutines
26
Calling a NEAR Proc
❖ The call instruction and the subroutine it calls are in the same segment
❖ The call instruction save the current value of the IP in the stack
Stack
SS:FFFD 00
13
3/2/2024
27
Calling a FAR Proc
❖ The call instruction and the subroutine it calls are in different segments
❖ The call instruction save the current value of the CS and IP in the stack
Stack
SS:FFF8 01
main PROC sub1 PROC FAR SS:FFF9 00
12B0:0018 MOV AX,1 12B0: 0080 PUSH AX
12B0:001A CALL FAR PTR sub1 … … SS:FFFA 1D
12B0:001D INC AX POP AX SS:FFFB 00
… RET
main ENDP sub1 ENDP SS:FFFC B0
SS:FFFD 12
28
Data Types and Data Definition
❖ DB (Define Byte)
▪ Allocation of memory in byte-sized chunks
❖ DW (Define Word)
▪ Used to allocate memory 2 bytes (one word) at a time
❖ DD (Define Doubleword)
❖ Used to allocate memory locations that are 4 bytes (two words) in size
❖ DQ (Define Quadword)
❖ Used to allocate memory locations that are 8 bytes (four words) in size
❖ DUP (Duplicate)
▪ DUP is used to duplicate a given number of characters
❖ EQU (Equate)
▪ Used to define a constant without occupying a memory location
▪ EQU can also be used outside the data segment, even in the middle of a code segment
❖ ORG (Origin)
▪ ORG is used to indicate the beginning of the offset address
14
3/2/2024
29
Example of Data Definition
30
More About Variables
15
3/2/2024
31
PTR Directive
32
More Examples (1) .MODEL SMALL
.STACK 64
.DATA
DATA_IN DB 25H,12H,15H,1FH,2BH
SUM DB ?
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
MOV CX,05
MOV BX,OFFSET DATA_IN
MOV AL,0
AGAIN: ADD AL,[BX]
INC BX
DEC CX
JNZ AGAIN
MOV SUM,AL
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
16
3/2/2024
33
More Examples (2)
.MODEL SMALL
.STACK 64 .CODE
.DATA MAIN PROC FAR
DATA_IN DW 234DH,1DE6H,3BC7H,566AH MOV AX,@DATA
ORG 10H MOV DS,AX
SUM DB ? MOV CX,04
MOV DI,OFFSET DATA_IN
MOV BX,00
ADD_LP: ADD BX,[DI]
INC DI
INC DI
DEC CX
JNZ ADD_LP
MOV SI,OFFSET SUM
MOV [SI],BX
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
34
More Examples (3)
.MODEL SMALL
.STACK 64 .CODE
.DATA MAIN PROC FAR
DATA_IN DB 25H,4FH,85H,1FH,2BH,0C4H MOV AX,@DATA
COPY DB 6 DUP(?) MOV DS,AX
MOV SI,OFFSET DATA_IN
MOV DI,OFFSET COPY
MOV CX,06H
MVLP: MOV AL,[SI]
MOV [DI],AL
INC SI
INC DI
DEC CX
JNZ MVLP
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
17