COAL Lecture Notes (After Mid)
COAL Lecture Notes (After Mid)
Machine code
1 Byte, 2 Byte, 4 Byte
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut
Introduction
In this lecture we are going to discuss that how machine codes are generated. When we
write and an instruction in assembly, then its equivalent machine code is generated by emu
8086. So, our concern is to know that how can we generate or find the machine code of an
assembly instruction manually.
So, let’s try to understand the whole procedure to find the machine code.
Machine code
Machine code of each instruction varies in number of bytes. Maximum size of a machine code or
an instruction can be 6 bytes. Each byte has its own format to consider when we are generating
machine code. Here keep in mind that the machine code of each instruction depends on the
considered memory or register through which we are generating machine code.
MOV BL, AL
For example, in above instruction, the machine code is 8AD8H if we consider BL register and
88C3H if we consider AL register. Here don’t worry about how the above codes are generated, the
main thing for you to pay the attention is that the machine code of each instruction depends on the
considered memory or register through which we are generating machine code.
So, now we understand the format of each byte starting from first byte.
1
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Here,
• W = 1 if considered register is 16 bit and 0 otherwise.
• D = 1 if considered register is destination and 0 otherwise.
• Opcode depends on operation and you can find it on internet (i.e., MOV, ADD, SUB
etc.).
MOV AX, BX
Here we have to find the machine code of above instruction and keep in mind that we are going to
consider AX register to generate code.
Here we have to write values for each portion of first byte. So,
OPCODE = 100010 (Operation code for MOV instruction is 100010)
D = 1 (because AX is destination)
W= 1 (because AX is 16 bit register)
2
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Here MOD means what is the second operand in instruction, REG means what is the name
(binary) of first considered register, and R/M means what is the name (binary) of second operand
(operand of step 1).
Values for all these portions will be found using following tables:
Here we again consider the previous instruction as an example in which we have found machine
code for first byte.
3
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Here,
MOD = 11 (because second operand, which is BX, is register with no displacement)
REG = 000 (because considered register is AX and value for AX register in table is 000)
R/M = 011 (because second operand is BX and its value in table is 011)
So here machine code for second byte will be written just like first byte:
MOD REG R/M = 11 000 011 = 1100 0011 = C3
So, we will get complete code for instruction MOV AX, BX after combining code of both first
and second byte because it was 2 Byte instruction. i.e.,
Complete code = 8BC3h (2 byte instruction)
Example 1
MOV DL, BH
Here we are considering DL register.
For first byte:
4
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Example 2
MOV BL, AL
5
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Example 3
MOV AL, [DI]
Here we will consider AL register.
For first byte:
MOD = 00 (because second operand, which is [DI], is memory with no displacement and its value
is table is 00)
REG = 000 (because considered register is AL and value for AL register in table is 000)
R/M = 101 (because second operand is [DI] and its value in above table is 101)
Second byte code = 00 000 101 = 0000 0101 = 05
Complete code = 8A05h ( 2byte instruction)
6
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Here is Byte 3 we will have to write Low displacement data and in Byte 4 we will have to write
High displacement data. For example in ADD [BX][DI]+1234H, AX instruction, we will have to
write 34 in Byte 3 and 12 in Byte 4.
Let’s understand this with an example.
Example 4
ADD [BX][DI]+1234H, AX
Here we are considering AX register.
For first byte:
7
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
So Low disp data should be 34 and high disp data should be 12.
Now we will get complete code by combining codes of all these bytes.
8
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Immediate Addressing
In previous topic, we were only using register or memories with different instructions. But now
we are going to use direct values or you can say immediate addressing. For example we were
dealing with instructions like MOV AX, BX but now we are going to discuss instructions like
MOV AX, 1. So here you can see that we are now moving direct value into AX which was not the
case in previous examples.
So, let us understand how machine codes are generated in immediate addressing.
Here you have to keep in mind that when you are dealing with immediate addressing, then the
format of bytes and operation code change depending on the operation being performed and you
can easily search about these formats and operation code on the internet. Because now you have
enough idea about how machine codes are generated, so I hope now you can generate machine
codes by just understanding the format of bytes.
For example the formats of bytes for MOV instruction are given below.
Here first byte is in orange color and second byte is in blue color
• 4 bits Opcode depends on operation and you can find it on internet (i.e., MOV, ADD,
SUB etc.)
• W = 1 if register is 16 bit and 0 otherwise.
• 3 bits Reg is binary of register which can be found using below table.
• Here in second byte data is the actual value which is being moved in the register.
9
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Here first byte is in orange color and 2nd and 3rd bytes are in blue color
Rest of the points are same as 2 byte format. Just difference is that if value consists of 4
characters then low byte data and high byte data will be placed in their respective bytes
just like as we did in previous example.
Here again I repeat that the above formats are just for MOV instruction/operation and these formats
may differ in case of ADD, SUB etc. operations.
Example 1
MOV AH, 1
Here we will consider register to generate code.
Opcode = 1011 (operation code for MOV instruction is 1011 in case of immediate
addressing).
W = 0 because AH is 8-bit register.
Reg = 100 for AH register in above table.
Data = 01 (it is the actual value which is being moved i.e, 1 in MOV AH, 1)
10
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Example 2
MOV AX, 1234h
Opcode = 1011 (operation code for MOV instruction is 1011 in case of immediate
addressing).
W = 1 because AX is 16-bit register.
Reg = 000 for AX register in above table.
Low byte = 34 and High byte = 12 because:
11
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
MUL Instruction
DIV Instruction
Decimal Input and Output Procedures
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut
Byte form:
For byte multiplication, one number is contained in the source and the other is assumed to be in AL. The
16-bit product will be in AX. The source may be a byte register or memory byte, but not a constant.
Word Form:
For word multiplication, one number is contained in the source and the other is assumed to be in AX. The
most significant 16-bits of the double word product will be in DX and the least significant 16 bits will be
in AX (we sometimes write this as DX:AX). The source may be a 16-bit register or memory word, but not
a constant.
For multiplication of positive numbers (0 in the most significant bit), MUL and IMUL give the same result.
12
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
For MUL CF/OF = 1 because DX is not 0. This reflects the fact that the product FFFE000h is too big to fit
in AX.
Simple Applications of MUL:
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV CX, 5
MOV AX, 1
FACT:
MUL CX
LOOP FACT
CALL DECOUT
MAIN ENDP
INCLUDE D:\DECOUT.ASM
END MAIN
13
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
DIVISION:
When division is performed, we obtain two results, the quotient and the remainder. As with multiplication,
there are separate Instructions for unsigned and signed division, DIV (divide) is used for unsigned division
and IDIV-(integer divide)-for signed division.
The syntax is:
DIV divisor
Byte Form:
In this form, the divisor is an 8-bit register or memory byte. The 16-bit dividend is assumed to be in AX.
After division, the 8-bit quotient is in AL and the 8-bit remainder is in AH. The divisor may not be a
constant.
Word Form:
Here the divisor is a 16-bit register or memory word. The 32-bit dividend Is assumed to be In DX : AX,
After division, the 16-bit quotient is in AX and the 16-bit remainder is In DX. The divisor may not be a
constant.
Divide Overflow:
It is possible that the quotient will be too big to fit in the specified destination (AL or AX). This can happen
if the divisor is much smaller than the dividend: When this happens, the program terminates and the system
displays 'the message "Divide Overflow"
DECIMAL INPUT:
To do decimal input, we need to convert a string of ASCII digits to the binary representation of a decimal
integer. We will write a procedure INDEC to do this. In procedure OUTDEC, to output the contents of AX
in decimal we repeatedly divided AX by 10. For INDEC we need repeated multiplication by 10.
Decimal Input Algorithm
total = 0
read an ASCII digit
REPEAT
convert character to a binary value
total = 10 x total + value
read a character
UNTIL character is a carriage return
14
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
15
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Decimal Output:
We will write a procedure OUTDEC to print the contents of AX as a signed decimal integer. If
AX >= 0, OUTDEC will print the contents in decimal; if AX < 0, OUTDEC will print a minus
sign, replace AX by -AX (so that AX now contains a positive number), and print the contents in
decimal.
Algorithm for Decimal Output
1. IF AX < 0 /*AX holds output value */
2. THEN
3. Print a minus sign
4. Replace AX by its two's complement
5. END IF
6. Get the digits in AX’s decimal representation
7. Convert these digits to characters and print them
To see what line 6 entails, suppose the content of AX, expressed in decimal, is 24168. To get the
digits In the decimal representation, we cal)proceed as follows:
Divide 24618 t1y 10. Quotient= 2461, remainder= 8
Divide 2461 by 10. Quotient= 246, remainder = 1
Divide 246 by 10. Quotient= 24, remainder= 6
Divide 24 by 10. Quotient 2, remainder = 4
Divide 2 by 10. Quotient = 0, remainder = 2
16
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
DECIMAL INPUT/OUTPUT
.MODEL SMALL JNGE NOT_DIGIT
.STACK 100H CMP AL, '9'
.CODE JNLE NOT_DIGIT
MAIN PROC
CALL DECIN AND AX, 000FH
PUSH AX
CALL DECOUT
MOV AH, 4CH MOV AX, 10
INT 21H MUL BX
MAIN ENDP POP BX
ADD BX, AX
DECIN PROC
PUSH BX MOV AH, 1
PUSH CX INT 21H
PUSH DX CMP AL, 0DH
JNE REPEAT2
BEGIN:
MOV AH, 2 MOV AX, BX
MOV DL, '?' OR CX, CX
INT 21H JE EXIT
NEG AX
XOR BX, BX
XOR CX, CX EXIT:
POP DX
MOV AH, 1 POP CX
INT 21H POP BX
RET
CMP AL, '-'
JE MINUS NOT_DIGIT:
CMP AL, '+' MOV AH, 2
JE PLUS MOV DL, 0DH
JMP REPEAT2 INT 21H
MINUS:
MOV CX, 1 MOV DL, 0AH
PLUS: INT 21H
INT 21H
JMP BEGIN
REPEAT2: DECIN ENDP
CMP AL, '0'
17
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
DECOUT PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
CMP AX, 0
JG ALPHA
PUSH AX
POP AX
NEG AX
ALPHA:
XOR CX, CX
MOV BX, 10D
WHILE_:
XOR DX, DX
DIV BX
PUSH DX
INC CX
CMP AX, 0
JNE WHILE_
MOV AH,2
MOV DL,10
INT 21H
MOV DL,13
INT 21H
MOV AH, 2
PRINT:
POP DX
ADD DL, 48
INT 21H
LOOP PRINT
POP DX
POP CX
POP BX
POP AX
RET
DECOUT ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
END MAIN
18
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
DIVI:
MOV DX,0 DIV BX
DIV BX MOV BX, DX
PUSH DX MOV AX, CX
INC CX CMP BX, 0
CMP AX,0 JNE WHILEE_
JNE DIVI
MOV HCF, AX
MOV AX,0 MOV CX, AX
ADDUP: MOV AX, NUM1
POP DX MOV BX, NUM2
ADD AX,DX
LOOP ADDUP MUL BX
DIV CX
CALL DECOUT MOV LCM, AX
MOV AH, 4CH CALL DECOUT
INT 21H MOV AH, 4CH
MAIN ENDP INT 21H
END MAIN MAIN ENDP
END MAIN
19
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Arrays
Addressing Modes
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut
This lecture will cover Arrays and Addressing Modes. To access an array in assembly language,
we use a pointer. A pointer is simply a register or variable that contains a memory address.
Most assembly language instructions require operands to be processed. When an instruction
requires two operands, the first operand is generally the destination, which contains data in a
register or memory location and the second operand is the source. Source contains either the
data to be delivered (immediate addressing) or the address (in register or memory) of the data.
Generally, the source data remains unaltered after the operation.
One-Dimensional Arrays:
A one-dimensional array is an ordered list of elements, all of the same type. By “ordered,” we
mean that there is a first element, second element, third element, and so on. In mathematics, if A
is an array, the elements are usually denoted by A[1], A[2], A[3] and so on. Figure shows a one
dimensional array A with six elements.
Example:
MSG DB ‘abcde’
20
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Or a word array of six integers, initialized to 10, 20, 30, 40, 50, 60 variable is called the base
address of the array. If the offset address assigned to W is 0200h, the array looks like this in
memory:
This operator causes value to be repeated the number of times specified by repeat_count. For
example:
GAMMA DW 100 DUP (0)
Creates an array of 212 uninitialized bytes. DUPs may be nested. For Example:
LINE DB 5,4, 3 DUP (2,3 DUP (0),1)
21
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Position Location
1 A
2 A=1XS
3 A=2XS
4 .
5 .
6 .
N A=(N-1)XS
In many applications we need to perform some operation on each element of an array. For example,
suppose array is a 10-element array, and we want to add the elements. In a high-level language,
we could do it like this:
SUM=0
N=1
REPEAT
SUM=SUM+A[N]
N=N+1
UNTIL N>10
To code this in assembly language, we need a way to move from one array element to the next
one. In the next section, we’ll see how to accomplish this by direct addressing.
22
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Addressing Modes:
The way and operand is known as its addressing mode. The addressing modes we have used so far
are:
1. Register mode: which means that an operand is a register.
2. Immediate mode: when an operand is a constant.
3. Direct mode: when an operand is a variable.
4. Register indirect Mode
In this mode, the offset address of the operand is contained in register. We say that the register acts
a pointer to the memory location.
The register BX, SI, DI, or BP. For BX, SI, or DI, the operand’s segment number is contained in
DS. For BP, SS has the segment numbers.
For example, suppose that SI contains 0100h, and the word at 0100h contains 1234h. To execute
MOV AX, SI
The CPU (1) examines SI and obtains the offset address 100h, (2) uses the address DS:0100h to
obtain the value 1234h, and (3) moves 1234h to AX. This is not the same as:
MOV AX, SI
Example: Suppose that
BX contains 1000h Offset 1000h contains 1BACh
SI contains 2000h Offset 2000h contains 20FEh
DI contains 3000h Offset 3000h contains 031Dh
Where the above offsets are in the data segment addressed by DS.
Tell which of the following instructions are legal. If legal, give the source offset address and the
result or number moved.
a. MOV BX, [BX]
b. MOV CX, [SI]
c. MOV BX, [AX]
d. ADD [SI], [DI]
e. INC [DI]
23
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Solution:
Source offset Result
a. 1000h 1BACh
b. 2000h 20FEh
c. Illegal source register (must be BX, SI or DI)
d. Illegal memory-memory Addition
e. 3000h 031Eh
Now let’s return to the problem of adding the elements of an array.
Example: Write some code to sum in AX the elements of the 10-elements array W defined by:
W DW 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
Solution: The idea is to set a pointer to the base of the array, and let it move up the array,
summing elements as it goes.
XOR AX, AX
LEA SI, W
ADDNOS:
ADD AX, [SI]
ADD SI, 2
LOOP ADDNOS
Example: Write a procedure REVERSE that will reverse an array of N words. This means that
the Nth word becomes the first, the (N-1) st word becomes the second, and so on, and the first
word becomes the Nth word. The procedure is entered with SI pointing to the array, and BX has
the number of words N.
Solution: The idea is to exchange the 1st and Nth words, the 2nd and (N-1) st word, and so on. The
number of exchanges will be N/2 (rounded down to the nearest integer if N is odd). Recall from
section 10.1 that the Nth element in a word array A has address A + 2 x (N - 1).
PUSH AX XCHG_LOOP:
PUSH BX MOV AX, [SI]
PUXH CX XCHG AX, [DI]
PUSH SI MOV [SI], AX
PUSH DI ADD SI, 2
; make DI point to nth word SUB DI, 2
MOV DI, SI LOOP XCHG_LOOP
MOV CX, BX POP DI
DEC BX POP SI
SHL BX, 1 POP CX
ADD DI, BX POP BX
SHR CX,1 POP AX
; swap elements REVERSE ENDP
24
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Tell which of the following instructions are legal. If legal, give the source offset address and
the number moved.
a. MOV AX, [ALPHA+BX]
b. MOV BX, [BX+2]
c. MOV CX, ALPAH[SI]
d. MOV AX, -2[SI]
e. MOV BX, [ALPHA+3+DI]
f. MOV AX, [BX]2
g. ADD BX, [ALPHA+AX]
25
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Solution:
Source offset Number moved
a. ALPHA+2 0456h
b. 2+2=4 2BACh
c. ALPHA+4 0789h
d. -2+4=2 1084h
e. ALPHA+3+1= ALPHA+4 0789h
f. Illegal form of source operand Illegal source register
Example:
Replace each lowercase letter in the following string by its upper-case equivalent. Use index
addressing mode.
Solution:
MOV CX, 17
XOR SI, SI
TOP:
CMP MSG[SI], ‘ ‘
JE NEXT
AND MSG[SI], 0DFh
NEXT:
INC SI
LOOP TOP
26
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
ADDUP: ADDUP:
ADD AX,[SI] ADD AX,[SI]
ADD SI, 2 ADD SI, 2
LOOP ADDUP LOOP ADDUP
CALL DECOUT
XOR DX, DX
MOV AH, 4CH MOV BX, 7
INT 21H DIV BX
MAIN ENDP
INCLUDE D:\DECOUT.ASM CALL DECOUT
END MAIN
MOV AH, 4CH
INT 21H
MAIN ENDP
INCLUDE D:\DECOUT.ASM
END MAIN
27
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
SEARCH NO NO OF OCCURRENCE
.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.DATA .DATA
MSG1 DB 10,13,"FOUND$" COUNT DW 0
MSG2 DB 10,13,"NOT FOUND$" X DW 100
X DW 100 ARR DW 800, 200, 100, 400, 500, 100, 700
ARR DW 800, 200, 100, 400, 500, 600, 700 .CODE
.CODE MAIN PROC
MAIN PROC MOV AX, @DATA
MOV AX, @DATA MOV DS, AX
MOV DS, AX
LEA SI, ARR
LEA SI, ARR MOV CX, 7
MOV CX, 7 MOV AX, X
MOV AX, X
ADDUP:
ADDUP: CMP AX, [SI]
CMP AX, [SI] JE PLUS
JE FOUND ADD SI, 2
ADD SI, 2 LOOP ADDUP
LOOP ADDUP JMP EXIT
28
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
MIN NO MAX NO
.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.DATA .DATA
ARR DW 800, 200, 700, 400, 900, 600, 100 ARR DW 800, 200, 700, 400, 900, 600, 100
.CODE .CODE
MAIN PROC MAIN PROC
MOV AX, @DATA MOV AX, @DATA
MOV DS, AX MOV DS, AX
ADDUP: ADDUP:
CMP BX, [SI] CMP BX, [SI]
JG SWAP JL SWAP
ADD SI, 2 ADD SI, 2
LOOP ADDUP LOOP ADDUP
SWAP: SWAP:
XCHG BX, [SI] XCHG BX, [SI]
ADD SI, 2 ADD SI, 2
LOOP ADDUP LOOP ADDUP
EXIT: EXIT:
MOV AX, BX MOV AX, BX
CALL DECOUT CALL DECOUT
29
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Linked list
Insert, update, delete
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut
Introduction
Basically, there are many data structures like arrays, linked lists, etc. Each sort of arrangement has its
strengths and weaknesses. For these reasons, it’s important to know the benefits and drawbacks of different
data structures when it comes to designing, optimizing, and scaling programs. In this lecture we are going
to discuss that what is linked list and how we can implement it in assembly.
So, let’s try to understand about linked list.
Linked List
A linked list is a linear data structure. It is a collection of nodes, and a node contains data and addresses the
next node.
Or in another words, A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection to another link.
Linked list is the second most-used data structure after array. It’s a set of structures ordered not by their
physical placement in memory (like an array) but by logical links that are stored as a part of the info within
the structure itself.
A linked list is another way to collect similar data. However, unlike an array, elements in a linked list aren’t
in consecutive memory locations. A linked list consists of nodes that are connected with one another using
pointers. The figure illustrates a linked list.
30
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
31
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
32
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Implementation in Assembly
Linked List
.MODEL SMALL
.STACK 100H
.DATA
ARR DB 10 DUP(?, -1) 0 1 2 3 4 5 6 7 8 9
P1 DW 0 ? -1 ? -1 ? -1 ? -1 ? -1
P2 DW 1
P3 DW 0
FD DW 0
M1 DB 10,13,"NUMBER NOT FOUND$"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV CX, 1
MOV AX, 55 0 1 2 3 4 5 6 7 8 9
;CALL DECINPUT 55 -1 ? -1 ? -1 ? -1 ? -1
CALL INSERT
55
CALL DISPLAY
CALL NEXT_LINE
MOV AX, 66 0 1 2 3 4 5 6 7 8 9
;CALL DECINPUT 55 2 66 -1 ? -1 ? -1 ? -1
CALL INSERT
55 66
CALL DISPLAY
CALL NEXT_LINE
MOV AX, 77 0 1 2 3 4 5 6 7 8 9
;CALL DECINPUT 55 2 66 4 77 -1 ? -1 ? -1
CALL INSERT 55 66 77
CALL DISPLAY
CALL NEXT_LINE
MOV AX, 88 0 1 2 3 4 5 6 7 8 9
;CALL DECINPUT 55 2 66 4 77 6 88 -1 ? -1
CALL INSERT 55 66 77 88
CALL DISPLAY
33
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
CALL NEXT_LINE
MOV AL, 77 0 1 2 3 4 5 6 7 8 9
CALL DELETE 55 2 66 6 77 6 88 -1 ? -1
CALL DISPLAY
55 66 88
CALL NEXT_LINE
MOV AX, 99
;CALL DECINPUT 0 1 2 3 4 5 6 7 8 9
CALL INSERT 55 2 66 6 77 6 88 8 99 -1
CALL DISPLAY 55 66 88 99
CALL NEXT_LINE
MOV AL, 66 0 1 2 3 4 5 6 7 8 9
CALL DELETE 55 6 66 6 77 6 88 8 99 -1
CALL DISPLAY 55 88 99
34
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
JMP TERM
DELF:
INC FD
INC FD
TERM:
MOV P3, 0
RET
DELETE ENDP
DISPLAY PROC
MOV DI, FD
35
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
PRINT:
XOR AX, AX
MOV SI, DI
MOV AL, ARR[DI]
;ADD DL, 48
;INT 21H
CALL DECOUTPUT
MOV AH, 2
MOV DL, "|"
INT 21H
INC SI
CMP ARR[SI], -1
JE EXIT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
NEXT_LINE PROC
PUSH AX
PUSH DX
MOV AH, 2
MOV DL, 10
INT 21H
MOV DL, 13
INT 21H
POP DX
POP AX
RET
NEXT_LINE ENDP
36
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
NEG AX POP DX
EXIT2: POP CX
POP DX POP BX
POP CX POP AX
POP BX
RET RET
Not_Digit: DECOUTPUT ENDP
MOV AH,2 END MAIN
37
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
In this lecture we will firstly cover the direction flag. One of the control flags in the direction flag
(DF). Its purpose is to determine the direction in which string operation will proceed. These
operations are implemented the two index registers will proceed. These operations are
implemented by the two index registers SI and DI. Then will proceed to load, store, compare and
scan string along with different programming examples.
If DF=0, SI and DI proceed in the creation of increasing memory addresses from left to right
across the string conversely, if DF=1, SI and DI proceed in the direction of decreasing memory
addresses: from right to left.
In the DEBUG display DI=0 is symbolized by UP, and DF=1 by DN.
38
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Moving a String:
Suppose we have defined two strings as follows:
.DATA
STRING1 DB ‘HELLO’
STRING2 DB 5 DUP (?)
And we would like to move the contents of STRING 1 (the source string) into STRING2 (the
destination string). This operation is needed for many string operations, such a) duplicating a
string or concatenating strings (attaching one string to the end of another string).
Copies the contents of the byte addressed by DS:SI, to the byte addressed by ES: DI. The contents
of the source byte are unchanged. After the byte has been moved, both SI and DI are automatically
incremented. If DF=0, or decremented If DF= I. For example, to move the first two bytes of
STRING1 to STRING2, we execute the following Instructions:
MOVSB is the first instruction we have seen that permits a memory- memory operation. It is also
the first instruction that involves the ES register.
39
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
The REP prefix causes MOVSB to be executed N times. After each MOVSB, CX is
decremented until it becomes 0. For example, to copy STRING1 of the preceding section into
STRING2, we execute:
CLD
LEA SI, STRING1
LEA DI, STRING2
MOV CX, 5 ; no. of chars in STRING1
REP MOVSB
Example: Write instructions to copy STRING 1 of the preceding section into STRING2 in
reverse order.
Solution: The idea is to get SI pointing to the end of STRING 1, DI to the beginning of
STRING2, then move characters as SI travels to the left across STRING1.
MOVSW moves a word from the source string to the destination string. Like MOVSB, it expects
DS:SI to point to a source string word, and ES:DI to point to a destination string word. After a
string word has been moved, both SI and DI are increased by 2 if DF = 0, or are decreased by 2 if
OF = 1. MOVSB and MOVSW have no effect on the flags.
40
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
STD
LEA SI, ARR+8h
LEA DI, ARR+8h
MOV CX, 3
PTR MOVSW
MOV WORD PTR [DI], 30
Store String:
The STOSB Instruction moves the contents of the AL register to the byte addressed b ES:DI. DI
is incremented if DF=0 or decremented if DF=1. Similarly, the STOSW instruction moves the
contents of AX to the word at address ES:DI and updates DI by 2, according to the direction flag
setting.
STOSB and STOSW have no effect on the flags.
As an example of STOSB, the following instructions will store two “A”s in STRING1:
41
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Load String:
The LODSB instruction moves the byte addressed by DS:SI into AL.SI is then incremented if
DF=O or decremented if DF=1. The word form is
LODSW. It moves the word addressed by DS:SI into AX;SI is incremented if By 2 if DF=O
or decreased by 2 if DF=1.
LODSB can be used to examine to characters of a string, as shown later.
LODSB and LODSW have no effect on the flags.
Scan String:
SCASB can be used to examine a string for a target byte. The target byte is contained in Al.
SCASW in this case; the target word is in AX.
Compare String:
The CMPSB Instruction CMPSB subtracts the byte with address ES:DI from the byte with
address DS:SI, and sets the flags. CMPSW it subtracts the word with address ES:DI from the
word whose address is DS:SI, and sets the flags.
42
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
43
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
44
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
45
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
46
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
FIND SUBSTRING
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "ENTER sUB STRING",10,13,"$"
MSG2 DB 10,13,"ENTER MAIN STRING ",10,13,"$"
MAINST DB 80 DUP (0)
SUBST DB 80 DUP (0)
STOP DW ? ;LAST PLACE TO BEGIN SEARCH
START DW ? ;PLACE TO RESUME SEARCH
SUB_LEN DW ?
YESMSG DB 10,13,"SUBSTRING IS IN MAIN STRING $"
NOMSG DB 10,13,"SUBSTRING IS NOT IN MAIN STRING $"
.CODE
MAIN PROC
47
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
READSTR PROC
PUSH AX
PUSH DI
CLD
XOR BX,BX
MOV AH,1
INT 21H
WHILE:
CMP AL,13
JE END_WHILE
CMP AL,8
JNE ELSE
DEC DI
DEC BX
JMP READ
ELSE:
STOSB
INC BX
READ:
INT 21H
JMP WHILE
END_WHILE:
POP DI
POP AX
RET
READSTR ENDP
END MAIN
48
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
49
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
50
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Macros
Macro Definition and Invocation
Label Conflict
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut
This lecture will cover Macros and label conflict along with different examples. A macro is a
block of text that has been given a name when MASM encounters the name during assembly, it
inserts the block into the program. The text may consist of instructions, pseudo-ops, comments, or
references to other macros.
Example:
Define a macro to move a word into a word.
Solution:
MOVW WORD1, WORD2
PUSH WORD2
POP WORD1
ENDM
51
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Where a1, a2, … an is a list of actual arguments. When MASM encounters the macro name,
it expands the macro; that is, it copies the macro statements into the program at the position
of invocation, just as if the user had typed them in. as it copies the statements, MASM
replaces each dummy argument di by the corresponding actual argument ai and creates the
machine code for any instructions.
Example:
Invoke the macro MOVW to move B to A, where A and B are word variables.
Solution:
MOV A, B
To expand this macro, MASM would copy the macro statements into the program at the
position of the call, replacing each occurrence of WORD1 by A, and WORD2 by B.
The result is:
PUSH B
POP A
In expanding a macro, the assembler simply substitutes the character strings defining the
actual arguments for the corresponding dummy ones. For example, the following calls
to the MOVW macro:
MOVW A, DX and MOVW A+2, B
Cause the assembler to insert this code into the program:
PUSH DX and PUSH B
POP A POP A+2
Label conflict:
A macro with a loop or decision structure contains one or more labels. If such a macro is Invoked
more than once In a program, a duplicate label appears, resulting in an assembly error: This
problem can be avoided by using local labels In the macro. To declare them, we use the LOCAL
pseudo-op, whose syntax is:
LOCAL list_of _ labels
Where list_of_labels is a list of labels, separated by commas. Every time the macro is expanded,
MASM assigns different symbols to the labels in the list.
The LOCAL directive must appear on the next line after the MACRO statement.
52
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
MACROS MACROS 1
.MODEL SMALL .MODEL SMALL
PRINT MACRO A,B PRINT MACRO A,B
MOV CX, A MOV CX, A
MOV DL, B MOV DL, B
MOV AH, 2 MOV AH, 2
DISP: DISP:
INT 21H INT 21H
LOOP DISP LOOP DISP
ENDM ENDM
.STACK 100H .STACK 100H
.CODE .CODE
MAIN PROC MAIN PROC
MOV BX, 10 PRINT 10, '*'
MOV AH, '*'
PRINT BX, AH MOV AH, 4CH
MOV AH, 4CH INT 21H
INT 21H
MAIN ENDP MAIN ENDP
END MAIN END MAIN
53
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
.STACK 100H
.DATA
MSG DB "OK$"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV BX, 10
MOV AH, '*'
MAX 30, 25
JMP EXIT
EXIT:
LEA DX, MSG
MOV AH, 9
INT 21H
54
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Inline Assembly
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut
The inline assembler is a feature of some compilers that allows very low-level code written
in assembly to be embedded in a high-level language. In this lecture inline assembly along with
different programming examples will be covered.
Factorial
#include "stdafx.h" system("pause");
#include <windows.h> return 0;
#include <iostream> }
using namespace std;
int fact( int num)
int fact(int num); {
int x;
int main() __asm
{ {
int n; mov eax, 1
char c='y'; mov ecx,num
do{ z:
system("cls"); mul ecx
cout<<"Enter Number for factorial : "; loop z
cin>>n;
cout<<endl<<fact(n)<<endl; mov x,eax
cout<<"Enter Again (y/n) ? : "; }
cin>>c; return x;
} }
while(c=='y' || c=='Y');
55
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Power of a number
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
int a,b;
char c='y';
do{
system("cls");
cout<<"Number = ";
cin>>a;
cout<<"power = ";
cin>>b;
cout<<a<<"^"<<b<<" = "<<power(a,b)<<endl;
cout<<"\n\nEnter Again (y/n) ? : ";
cin>>c;
}
while(c=='y' || c=='Y');
system("pause");
return 0;
}
56
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
57
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
}
return x;
}
int subtraction(int a,int b)
{
int x;
__asm {
mov eax,a
sub eax,b
mov x,eax
}
return x;
}
int multiplication(int a,int b){
int x;
__asm {
mov eax,a
mov ebx,b
mul ebx
mov x,eax
}
return x;
}
int division(int a,int b){
int x;
__asm {
xor edx, edx
mov eax, a
div b
mov x,eax
}
return x;
}
58
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Recursion
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut
In this lecture recursion will be covered. A recursive procedure is one that calls itself. There are two kind
of recursion: direct and indirect. In direct recursion, the procedure calls itself and in indirect recursion, the
first procedure calls a second procedure, which in turn calls the first procedure.
A recursive procedure is a procedure that calls itself. Recursive procedures are important in high-level
languages, but the way the system uses the stack to implement these procedures is hidden from the
programmer. In - assembly language, the programmer must actually program the stack operations. So this
provides, an opportunity to see how recursion really works.
MOV AX, 3
PUSH AX
CALL ADDITION
CALL DECOUT
ADDITION PROC
PUSH BP
MOV BP, SP
59
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
END_IF:
MOV CX, [BP + 4]
DEC CX
PUSH CX
CALL ADDITION
RETURN:
POP BP
RET 2
ADDITION ENDP
INCLUDE D:\DECIN.ASM
INCLUDE D:\DECOUT.ASM
MAIN ENDP
60
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Factorial
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AX, 5
PUSH AX
CALL FACTORIAL
CALL DECOUT
FACTORIAL PROC
PUSH BP
MOV BP, SP
MOV AX, 1
JMP RETURN
END_IF:
MOV CX, [BP + 4]
DEC CX
PUSH CX
CALL FACTORIAL
RETURN:
POP BP
RET 2
FACTORIAL ENDP
INCLUDE D:\DECIN.ASM
INCLUDE D:\DECOUT.ASM
MAIN ENDP
61
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587
Searching
.MODEL SAMLL
.STACK 100H
.DATA
Max DW 3, 5, 2, 4
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AX, 4
PUSH AX
CALL Find_Max
CALL DECOUT
Find_Max PROC
PUSH BP
MOV BP, SP
CALL Find_Max
CMP Max[BX], AX
JGE END_IF
62