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

MP03_Machine-code-and-assembly-language-1

This document provides an introduction to microprocessors, focusing on machine code and assembly language, specifically the 8086 instruction format. It explains the differences between high-level and low-level programming languages, the structure of assembly language statements, and how to write assembly programs using Microsoft assembler. Additionally, it covers the roles of assemblers and linkers in converting source code to executable files.

Uploaded by

asmeissas942
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)
2 views

MP03_Machine-code-and-assembly-language-1

This document provides an introduction to microprocessors, focusing on machine code and assembly language, specifically the 8086 instruction format. It explains the differences between high-level and low-level programming languages, the structure of assembly language statements, and how to write assembly programs using Microsoft assembler. Additionally, it covers the roles of assemblers and linkers in converting source code to executable files.

Uploaded by

asmeissas942
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/ 16

Introduction to Microprocessors

Chapter 3 - Machine Code and Assembly


Language

Dr. Adnan Ismail Al-Sulaifanie

Department of Electrical and Computer Engineering


University of Duhok, Iraq

Outline

ä Explain the 8086 instruction format and how to convert assembly language state-
ment to machine code.

ä Understand differences between high level language and assembly language.

ä How to write an assembly language program using Microsoft assembler (MASM).

References

1. Brey, The Intel Microprocessors, chapters 4, 7 & 8.

2. Triebel, 8088 and 8086 microprocessors: Programming, Interfacing, Hard-


ware, chapter 7

3. Rafiquzzaman, Fundamentals of Digital Logic and Microcomputer Design


, chapter 9.

4. Mazidi, 80X86 IBM PC and Compatible Computers Assembly Language,


Design, and Interfacing, chapter 2.

1 8086 Instruction Format

ä Each instruction in assembly language represents a specific code (machine code).

1
ä Machine code is a binary code that the microprocessor understand and accord-
ingly executes the instruction.

ä To execute an instruction, it is required to provide the processor with the following


information: type of operation, operand size, operand addresses, and destination
operand.

ä 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:

Example 1 - the length of machine code in 8086 processor is variable and it


ranges from 1 to 6 bytes.

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

Example 2 - find machine code of following instructions:

opcode(MOV) = 100010 , opcode(ADD) = 000000

No. Instruction opcode D W MOD REG R/M Machine code


1 MOV AX, [SI] 100010 1 1 00 000 100 8B 04
2 ADD AX, [BX] 000000 1 1 00 000 111 03 07
3 ADD [BX+DI+1234], AX 000000 0 1 10 000 001 01 81 34 12
4 MOV CL, [2211] 100010 1 0 00 001 110 8A 0E 11 22
5 MOV BL, CL 100010 1 0 11 001 011 8A CB
100010 0 0 11 011 001 88 D9
6 ADD CL, DL 000000 0 0 11 010 001 00 D1
000000 1 0 11 001 010 02 CA

ä 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)

ä Sometimes the operation can be performed using different instructions. In this


case, short instruction is preferred because it requires less memory and runs
faster. For example if we need to reset the content of AX register, we have two
options:

1. MOV AX, 0000 ==> B8 00 00


2. XOR AX, AX ==> 31 C0

Therefore, the second option is preferred.

2 Programming Languages

ä Programming languages can be divided into two types:

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

ä Designing an intelligent compiler requires detailed understanding of the proces-


sor architecture.

Assembly Language High Level Language


Source code less number of statements
Generated code less size and run faster
Complexity complex to learn, write, and debug
Capability can directly access hardware
Compatibility run on different platforms
Applications new OS and device drivers user applications

Why should learn assembly language

ä 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

Each statement in assembly language has 1- 4 fields:

Label: opcode operand(s) ; comment

All fields ===> next: CMP AL, BL ; compare content of AL register with BL register

Three fields ===> ADD AX, DX ; add AX with BX

Two fields ===> INC AL

One field ===> CLC

All fields are optional except opcode as shown in previous line. Opcode can take one
of the following forms:

1. 8086 instruction.

2. Assembler directives =⇒ instructs assembler/linker about how the program


should be created. They do not generate any machine code.

3. Pseudo operation: =⇒ used to allocate some memory locations to save operands.

3.1 Data Pseudo Operations

ä 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”).

ä DB defines a variable name of type byte.


var-name DB value/? ====> A DB 65H , X DB ? (uninitialized)

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)

ä DUP command is used to initialize an array with same value


table1 DB 10 DUP(00) , table2 DB 50 DUP(?)

ä DW has similar function of DB and it is used define variables of type word (B DW


5A65H)

ä If the first digit of a value is letter, it should preceded by 0 (B DB 0FFH)

3.2 Segment Definition

3.2.1 Simplified Segment Definition

ä Simplified segment directives hide many details of segment definition and as-
sume the same conventions that used by high-level languages.

ä It is not necessary to assign a name to each segment.

ä Some common directives used in simplified segment definition are: .CONST,


.MODEL, .CODE, .DATA, and .STACK.

ä 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

Define constants here

.MODEL memory model

.STACK stack size

.DATA ; begining of data segment

Define data here

.CODE ; beginning of code segment

[ 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

3.2.2 Procedure Definition

proc-name PROC [near/far]

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 SI,OFFSET A → load SI register with the offset of variable A.

MOV CL,TYPE A → load CL register with the data type of variable A. CL = 1 for byte
size and 2 for word size.

Example 3 - write a complete assembly language program using simplified model


to compute 3E + F5 and store the result in memory.

.MODEL SMALL
.STACK 100H
.DATA
N1 DB 3Eh
N2 DB F5h
N3 DW ?
.CODE

START: MOV AX, @DATA


MOV DS,AX
MOV AL, N1
XOR AH, AH
ADD AL, N2
ADC AH, 00
MOV N3, AX
MOV AH,4CH
INT 21H

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.

Example 4 - write a program to multiply two arrays (A and B) element by element.

A is array of ten bytes (1, 2, 3, 4, 5, 6, 7, 8, 9, and A).


B is array of ten bytes (0F, 0F, 0F, 0F, 0F, 0F, 0F, 0F, 0F, 0F).
C=A*B

.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

START: MOV AX, SEG A

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

Example 5 - write a complete assembly program to compute the average of ten


unsigned words.

.MODEL small
.STACK 100H
.DATA
A DB 0A01h, 22FDh, 5344h, 0F14h, 6C15h, 0FF60h, 0FFB7h, 9879h, 0FFC9h, 0A67h
RES DW 10 dup(?)
.CODE

START: MOV AX, SEG A


MOV DS, AX ; load DS with segment address
MOV SI, OFFSET A ; load starting address of variable A into SI
XOR AX, AX ; AX = 0000
MOV DX, DX ; DX = 0000
MOV CX, 000Ah
NEXT : ADD AX, [SI]
ADC DX, 0000
ADD SI, 0002 ; add 2 to index next word
LOOP NEXT
MOV CX, 000Ah
DX AX
DIV CX ;
CX
MOV RES, AX
MOV AH, 4Ch
INT 21h
END START

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

START: MOV AX, SEG A


MOV DS, AX ; load DS with segment address
MOV SI, OFFSET A ; load starting address of variable A into SI
XOR AX, AX ; AX = 0000
MOV CX, 0064h ;= 100 in decimal
NEXT : TEST CX, 0001
JNZ NEXT2
ADD AX, CX
NEXT2 LOOP NEXT
MOV RES, AX
MOV AH, 4Ch
INT 21h
END START

Example 7 - write a complete assembly language program to fill the segment


1000h with pattern 00, 01, ... , FF, 00, 01, .. .

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

C(n) = A2 (n) − B(n)

.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

Example 10 - write a program to find the average of negative numbers in array


of 50 bytes.

.MODEL small
.STACK 100H
.DATA
A DB 0Ah, 01h, 22h, ...
B DB ?
.CODE

START: MOV AX, SEG A


MOV DS, AX ; load DS with segment address
MOV SI, OFFSET A
MOV CX,0032
XOR DL, DL
XOR BX, BX
XOR AX, AX
NEXT1: MOV AL, [SI]
TEST AL, 80H
JNZ NEXT2
ADD BX, AX
INC DL
NEXT2: INC SI

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

Example 12 - write a program to reset segment 1200 using string instructions.

.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

After executing program: 1500:0000 00 01 02 03 .... 45 66

Homework - write a program to compute the average of positive and negative


numbers in the array of 100 bytes. Store the result in memory locations 600-601.

15
Assume the array starting at address 500h.

Homework - an array contains 100 bytes. Write a program to construct another


array according to the following relation: y = x2 − 2x + 1

Homework - write a complete assembly language program to computes the sum


of numbers that divisile by 05 in memory range 1A00 to 1AF0

16

You might also like