MP03_Machine-code-and-assembly-language-1
MP03_Machine-code-and-assembly-language-1
Outline
ä Explain the 8086 instruction format and how to convert assembly language state-
ment to machine code.
References
1
ä Machine code is a binary code that the microprocessor understand and accord-
ingly executes the instruction.
ä Figure below shows the structure of machine code that used by most instructions.
It is applicable for instructions that uses register addressing mode or register
addressing mode with memory addressing mode. The main fields show how the
previous information are represented:
2
Instruction Machine code Size (byte)
CLC (clear carry flag) F8 1
XOR AL, DH 30F0 2
ADD AH, [SI+05] 026405 3
MOV DL, [DI + 98CF] 8A95CF98 4
MOV byte ptr [SI+FFEE], 00 C684EEFF00 5
MOV word ptr [BX+1F00], 33AA C787001FAA33 6
ä The previous structure cannot be applied on all instructions because some in-
structions use different addressing mode. For example, the instruction MOV
word ptr [BP+DI+1234], A0B2 uses immediate and memory addressing modes.
Therefore it has special format:
1100 011W MOD 000 R/M offset data ==> 1100 0111 1000 0011 34 12 B2 A0
(C7 83 34 12 B2 A0)
2 Programming Languages
3
1. High Level Language (HLL) ==> C++, Matlab, Pascal, ...
2. Low level language (assembly language) ==> Microsoft assembler (MASM)
and Turbo assembler (TASM)
ä The program in HLL uses special statements. Therefore, a special software (com-
piler) is used to translate each statement to a number of instructions (or directly
to machine codes).
ä The compiler may generate many assembly language instructions for each state-
ment. For example translating the statement z = x + y in C++ requires three
instructions:
MOV AX, [X]
ADD AX, [Y]
MOV [Z], AX
ä Sometimes assembly language produces small size program which can be run
faster than compiler-based generated code.
ä Assembly language allows direct access to hardware features of the system. This
might be difficult or impossible with high level language.
4
ä Programming in assembly helps one to gain a deeper understanding of how com-
puters work. It also helps to understand how compilers and high level languages
like C work.
3 Assembly Language
All fields ===> next: CMP AL, BL ; compare content of AL register with BL register
All fields are optional except opcode as shown in previous line. Opcode can take one
of the following forms:
1. 8086 instruction.
ä The primary function of these commands is to define values for constant, vari-
ables, and labels. The most commonly used pseudo operations are:
Pseudo-ops Meaning Function Example
EQU Equate Assign value to a symbol AA EQU 06
DB Define byte Define byte size variable or locations AA DB 98
DW Define word Define word size variable or locations BB DW 0812h
ä EQU can be assigned a number (1,-2), character (’d’), and string (”hello world”).
5
ä DB is used to define a single element or an array
table DB 05H, 12H, 30H, ... (the size of array depends on the number of ele-
ments)
ä Simplified segment directives hide many details of segment definition and as-
sume the same conventions that used by high-level languages.
ä Every program uses simplified segments must begin with the .MODEL directive.
This directive defines number of segments used in the program.
ä The general structure of program that use simplified segment shown below:
.CONST
[ ORG starting address ] ; define offset address of first instruction (example ORG
LABEL: MOV AX, @DATA
6
MOV DS, AX ; initialize data segment
...
...
last instruction
MOV AH, 4CH
INT 21H ; return to the operating system
Define subroutine here
END [LABEL] ; this directive is used to close code segment
ä When new segment is defined the previous segment is automatically closed ex-
cept the last segment. Therefore, at the end of last segment the directive END
is used.
ä When the program is loaded in memory, the operating system load CS:IP and
SS:Sp registers while the programmer should load DS register with the starting
address of data segment.
ä MODEL directive defines number of code and data segment used in the program.
It has the following options: TINY, SMALL, COMPACT, MEDIUM, LARGE, HUGE,
or FLAT
statements
RET/RETF
proc-name ENDP
7
3.2.3 Value-returning and attribute operator
Some commands ar used to return the attribute such as segment address , offset
address, or data type.
MOV AX, SEG A → load AX register with the segment address where the variable A
is located in.
MOV CL,TYPE A → load CL register with the data type of variable A. CL = 1 for byte
size and 2 for word size.
.MODEL SMALL
.STACK 100H
.DATA
N1 DB 3Eh
N2 DB F5h
N3 DW ?
.CODE
END START
8
4 Assembler and Linker
ä Assembler (masm.exe) converts the source code to object code (.obj) and op-
tionally generates two files: listing file (.lst) and cross reference file (.crf).
ä Linker (link.exe) combine the .obj file with necessary library functions and gen-
erate final executable file (.exe). It can optionally generate map file (.map)
ä Object file (.obj) contains machine code of the source program. It can not be
directly run on the computer, so it must be processed by the linker to create an
executable file.
ä Listing file (.lst) contains the assembled version of the source file, hexadecimal
machine codes, and other information about the source file.
ä Cross reference file (.crf) lists the number of each line in the source program
at which each symbol is defined and the number of each line in which it is refer-
enced.
ä Map file (.map) show the start address, end address, and length of each memory
segment employed by the program that was linked.
.MODEL small
.STACK 100H
.DATA
A DB 1,2,3,4,5,6,7,8,9,0Ah
B DB 10 dup(0Fh)
RES DW 10 dup(?)
.CODE
9
MOV DS, AX ; load DS with segment address
MOV SI, OFFSET A ; load address of variable A into SI
MOV DI, OFFSET B
MOV BX, OFFSET RES
MOV CX, 000Ah
NEXT : MOV AL, [SI]
MUL BYTE PTR [DI]
MOV [BX], AX
INC SI
INC DI
ADD BX, 0002 ; increment BX by two because the operand size is 16 bit
LOOP NEXT
MOV AH, 4Ch
INT 21h
END START
.MODEL small
.STACK 100H
.DATA
A DB 0A01h, 22FDh, 5344h, 0F14h, 6C15h, 0FF60h, 0FFB7h, 9879h, 0FFC9h, 0A67h
RES DW 10 dup(?)
.CODE
10
Example 6 - write a complete assembly program to compute the sum of even
numbers in the range 1 to 100.
.MODEL small
.STACK 100H
.DATA
RES DW 10 dup(?)
.CODE
.MODEL tiny
.STACK 100H
.CODE
ORG 100h
START: MOV AX, 1000h
MOV DS, AX
MOV DI, 0000
XOR AL, AL ; AX = 00
MOV CX, 0000
NEXT : MOV [DI], AL
INC DI
INC AL
LOOP NEXT
MOV AH, 4Ch
INT 21h
END START
11
Example 8 - write a complete assembly program to arrange the numbers in a
table contains 10 bytes.
.MODEL small
.STACK 100H
.DATA
A DB 13h, 0A2h, 33h, 04h, 0F5h, 6Fh, 70h, 82h, 0A9h, 0Ah
RES DB ?
.CODE
START: MOV AX, SEG A
MOV DS, AX ; load DS with segment address
MOV SI, OFFSET A ; load address of variable A into SI
MOV DX, 0009h
NEXT1: MOV CX, DX
MOV DI, SI
INC DI
MOV AL, [SI]
NEXT2 : CMP AL, [DI]
JC NEXT 3
XCHG AL, [DI]
MOV [SI], AL
NEXT3: INC DI
LOOP NEXT2
INC SI
DEC DX
JNZ NEXT1
MOV AH, 4Ch
INT 21h
END START
Example 9 - two arrays A and B, each has 10 elements (8 bit signed number).
Write a program to construct another array (C) defined as follows:
.MODEL small
.STACK 100H
.DATA
A DB 0Ah, 01h, 22h, 0FDh, 53h, 44h, 0Fh, 14h, 6Ch, 15h
B DB 0FFh, 60h, 0Fh, 0FBh, 98h, 79h, 0FFh, C9h, 0Ah, 67h
RES DW 10 dup(?)
.CODE
12
START: MOV AX, SEG A
MOV DS, AX ; load DS with segment address
MOV SI, OFFSET A
MOV DI, OFFSET B
MOV BX, OFFSET RES
MOV CX,0032
NEXT: MOV AL,[SI]
IMUL AL
SUB AL,[DI]
SBB AH,0000
MOV [BX],AX
INC SI
INC DI
ADD BX,0002
LOOP NEXT
MOV AH, 4Ch
INT 21h
END START
.MODEL small
.STACK 100H
.DATA
A DB 0Ah, 01h, 22h, ...
B DB ?
.CODE
13
LOOP NEXT1
MOV AX, BX
IDIV DL
MOV B, AL
MOV AH, 4Ch
INT 21h
END START
Example 11 - write a program to move a block of data (100 bytes) from memory
address 2000:0100 into 3000: 0500.
.MODEL tiny
.STACK 100H
.CODE
ORG 100h
START: MOV AX, 2000h
MOV DS, AX
MOV SI, 0100h
MOV AX, 3000h
MOV ES, AX
MOV DI, 0500h
CLD
MOV CX, 100
REP MOVSB
MOV AH, 4Ch
INT 21h
END START
.MODEL tiny
.STACK 100H
.CODE
14
ORG 100h
START: MOV AX, 1200h
MOV ES, AX
MOV DI, 0
CLD
MOV CX, 0
XOR AL, AL ; AL = 0 ;
REP STOSB
MOV AH, 4Ch
INT 21h
END START
Example 13 - write a program to shift the content of segment 1500h one byte
down using string instructions.
.MODEL tiny
.STACK 100H
.CODE
ORG 100h
START: MOV AX, 1500h
MOV DS, AX
MOV ES, AX
MOV SI, 0FFFEh ; SI index first element in the segment
MOV DI, 0FFFFh ; DI index second element in the segment
MOV CX, 0FFFFh ; number of iterations is FFFF - 1
STD
NEXT: LODSB ; load memory address indexed by SI into AL
STOSB ; store AL in memory address indexed by DI
LOOP NEXT
MOV byte ptr [0000], 0 ; store 0 in first memory address
MOV AH, 4Ch
INT 21h
END START
Assume the content of the segment before executing program is: 1500:0000 01 02
03 .... 45 66 FF
15
Assume the array starting at address 500h.
16