0% found this document useful (0 votes)
5 views

5-Assembly Programming

The document provides an overview of assembly programming, detailing the structure and components of assembly language programs, including instruction formats, directives, and segments. It includes examples of assembly code, model definitions, and the process of building executable programs. Additionally, it discusses control transfer instructions and conditional jumps within assembly language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

5-Assembly Programming

The document provides an overview of assembly programming, detailing the structure and components of assembly language programs, including instruction formats, directives, and segments. It includes examples of assembly code, model definitions, and the process of building executable programs. Additionally, it discusses control transfer instructions and conditional jumps within assembly language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

3/2/2024

Microprocessor

Mohammad Hosseini

Assembly Programming

2
Assembly Language Programs
❖ A series of statements (lines)

➢ Assembly language instructions (ADD, MOV, etc.)


▪ Perform the real work of the program

➢ Directives (pseudo-instructions)
▪ Give directions to the assembler about how it should translate the
Assembly language instructions into machine code

❖ Consists of multiple segments

1
3/2/2024

3
Instruction Format

[label:] mnemonic [operands] [;comment]


Brackets indicate that the field is optional. Do not type in the brackets.

❖ 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.

❖ The comment field begins with a “;”


✓ Comments may be at the end of a line or on a line by themselves
✓ The assembler ignores comments

4
Directives

❖ Directives are used by the assembler to organize the program

❖ Do not generate any machine code

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

➢ Segments are defined using


SEGMENT and ENDS
directives

➢ The ASSUME directive


associates segment registers
with specific segments
➢ Tells the assembler which of the
segments defined by the
SEGMENT directives should be
used
➢ DOS initializes CS and SS
➢ DS must be initialized by the program

16
Build Procedure

➢ .asm: the source file Editor

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

➢ .map: name of the segments, their address Linker


and size
myfile.map
➢ .exe: ready-to-run (executable) program myfile.exe

8
3/2/2024

17
Example of lst and map

18
Tools

➢ MASM32 (Microsoft Assembler): x86 assembler and linker

➢ DOSBox: MS-DOS emulator

➢ EMU8086: Assembler, Linker, 8086 Emulator and Debugger

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

0005 8A 47 02 AGAIN: MOV AL,[BX]+2


0008 3C 61 CMP AL,61H
000A 72 06 JB NEXT
000C 3C 7A CMP AL,7AH
000E 77 02 JA NEXT
0010 24 DF AND AL,0DFH
0012 88 04 NEXT: MOV [Sl],AL

The assembler will generate a "relative


jump out of range" message if the
target address is out of the range (-128
“above” and “below” refer to the relationship of two unsigned values; to 127 bytes from the IP)
"greater" and "less" refer to the relationship of two signed values.

22
Unconditional Jump

JMP [SHORT|NEAR|FAR] label

❖ Control is transferred unconditionally to the target location label

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

❖ PROC and ENDP are used to define a subroutine


➢ Start and end of the procedure require a label

❖ PROC is followed by a range definition


➢ NEAR: procedure is defined within the same code segment with the caller
➢ FAR: procedure is defined outside the current code segment of the caller

❖ CALL is used to call a subroutine


➢ The microprocessor automatically saves the address of the instruction following the
call on the stack.
➢ The last instruction in the called subroutine must be RET, which restores the return
address from the stack

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

main PROC sub1 PROC SS:FFF9


12B0:0018 MOV AX,1 12B0: 0080 PUSH AX
12B0:001A CALL sub1 … … SS:FFFA 01
12B0:001D INC AX POP AX SS:FFFB 00
… RET
main ENDP sub1 ENDP SS:FFFC 1D

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

❖ Get the segment value of a variable

➢ MOV AX, SEG VAR1

❖ Get the offset address of a variable

➢ MOV AX, offset VAR1


or
➢ LEA AX, VAR1

15
3/2/2024

31
PTR Directive

❖ Temporarily change the type attribute of a variable


❖ To guarantee that both operands in an instruction match

DATA1 DB 10H, 20H, 30H


DATA2 DW 1234H

MOV BX, WORD PTR DATA1 ; 2010H -> BX


MOV AL, BYTE PTR DATA2 ; 34 -> AL

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

You might also like