Examples Mid
Examples Mid
1
Dr. Qurban Ali, EE Department
Example of Generating a Physical Address
2
Dr. Qurban Ali, EE Department
Segment and Offset values Illustration
There are many possible combinations of segment base
address and offset that yield the same physical address
(how?)
Example: 12B0:0025 = 12B25
12AF:0035 = 12B25
3
Dr. Qurban Ali, EE Department
Example
What is the data word shown in the figure below? Express the
result in hexadecimal form. Is it stored at an even- or odd-address
word boundary? Is it aligned or misaligned word of data?
Address Memory
(binary)
Solution:
◼ Most significant byte at 0072B is 7D16
◼ Least significant byte at 0072A is 0A16
◼ Together the two bytes give the word 7D0A16
◼ Since least significant byte is stored at address 0072A which is
in binary 0000 0000 0111 0010 1010. Hence it is stored at an
even boundary
◼ Therefore it is aligned.
4
Dr. Qurban Ali, EE Department
Memory Address Space : 4-byte word
❑ For double words (4 bytes), the least significant byte at the
lowest byte address, while the most significant byte at the highest
byte address.
❑ Aligned: A double word starting at address a multiple of 4
❑ Misaligned: A double word starting at address not a multiple of 4
❑ Example:
Address Memory Memory
(binary) (hexadecimal)
(0072E)16 0000 0101 05
(0072D)16 0001 1010 1A
(0072C)16 0010 0111 27
(0072B)16 0011 1011 3B
at memory address (0072B) is given by
051A273BH. Aligned or misaligned?
5
Dr. Qurban Ali, EE Department
Examples of Paging Mechanism
(a) Linear address=00400023, directory=?, page table =?
(b) Linear address=50000000, CR3=00004, paging directory address=?
(c) Linear address=07026112, Physical address=30014002, CR3=00004;
Find all missing information.
015602
Immediate Operand Addressing Mode
AX
15
11
Dr. Qurban Ali, EE Department
Indexed Addressing Mode Example:
MOV AL, [SI]+2000H
Similar to base addressing, it allows the use of a signed
displacement.
AX
AB
Note: Also, mov [BX][SI], AH
Dr. Qurban Ali, EE Department mov [BX+SI], AH 13
Review Example
Example:
If SS = 010516 and SP = 000816 then,
Address of bottom of stack = 105016 + FFFE16
= 1104E16
Address of top of stack = 105016 + 000816
= 105816
Address of end of stack = 105016 + 000016
= 105016
15
Dr. Qurban Ali, EE Department
Example of Pushing onto the Stack
105C 105C
TOS 105A TOS 105A
1058 1058
1056 1056 CD AB
1054 1054
1052 1052
1050 1050
01 05 SS 01 05 SS
00 08 SP 00 06 SP
16
Dr. Qurban Ali, EE Department
Example of Popping of the Stack
01 05 SS 01 05 SS
0008 SP 00 0A SP 17
Dr. Qurban Ali, EE Department
Exercise
(a) Consider bottom and end of the stack as FFFEH, and 0000H
respectively. If SS = 012216 and SP = 001216, what is the
address of bottom, top and end of the stack? Show the status of
stack, and AX register after following instructions are executed:
(b) How much data is in stack, and how much is it empty?
PUSH FF21
PUSH DC32
POP AX
PUSH 2A20
PUSH 3F12
PUSH 8DC1
PUSH 0098
POP BX
POP DX
PUSH 3512
PUSH 6236
PUSH AX
18
Dr. Qurban Ali, EE Department
Examples of using LEA, LDS and LES
20
Dr. Qurban Ali, EE Department
Review Example
06721 10
Figure 4–19 The operation of XLAT instruction at the
point just before 6DH is loaded into AL
22
OUTS
Transfers a byte, word, or double word of data from
the data segment memory location addressed by SI to
an I/O device - OUTSB; OUTSW; OUTSD
◼ I/O device is addressed by the DX register
Example: Write instructions to output string of 200
bytes to an I/O device at address 3AC from memory
address starting at ‘array’, using word at a time.
ADD [BX][SI]+1234H, AX
29
Dr. Qurban Ali, EE Department
Other Examples
Push BX
Means “write the word content of BX to the contents of
memory location specified by based-indexed addressing
mode”
Pop DX
Means “read the word content from top of the stack
segment of memory to register DX”
JNZ NXTPT
This has the form: 01110101 (IP-INC8)
01110101 (?)
0111 0101 ? ? = 75??16
NOP
This has the form:
10010000
9 0 = 9016
31
Dr. Qurban Ali, EE Department.
Example
MOV DS, AX
ADD AX, [SI]
MOV AX, 10
MOV BX, 0010
There: XOR CL, [1234H]
ADD [BX][SI]+1234H, AX
PUSH BX
INC SI
DEC BX
JNZ There
NOP
32
Dr. Qurban Ali, EE Department.
Example of Add Instruction
33
Dr. Qurban Ali, EE Department
Example of ADC
MOV AL, DL
ADD AL, BL
DAA
MOV DL, AL
MOV AL, DH
ADD AL, BH
DAA
MOV DH, AL
MOV CX, DX
36
Dr. Qurban Ali, EE Department
Example of SUB
37
Dr. Qurban Ali, EE Department
Example of SBB
39
Dr. Qurban Ali, EE Department
Example of AAS
43
Dr. Qurban Ali, EE Department
32-Bit and 64-bit Multiplication
45
Dr. Qurban Ali, EE Department
IDIV
Negative Numbers
IDIV performs signed division in 2’s complement,
its operation is similar to DIV
Example:
Let’s say you want to divide -2 by 2. Obviously, the result
should be -1.
So, if AL = -1 = FF and BL = 2, then
◼ DIV BL will give the result 7F with a remainder of 1.
What to do?
◼ Use IDIV
46
Dr. Qurban Ali, EE Department
Example of IDIV
How 8-bit number by an 8-bit number?
◼ In order to get the correct result we need to first sign
extend the 8 bit number and then use IDIV
To sign extend a byte to a word, use CBW
To sign extend a word to double word, use CWD
So, if AL = FF, CBW will cause AX = FFFF (which is -1 in
16 bits). Now, you can use IDIV BL
Example: Divide 4F by 2C
Procedure: MOV AL, 4F
CBW
MOV BL, 2C
DIV BL
Note: CBW causes AL to be extended into (AX) and CWD
cause AX to be extended into (DX)(AX) 47
Dr. Qurban Ali, EE Department
Another Example of IDIV
Example: Divide -2 by -1
48
Dr. Qurban Ali, EE Department
Exercise
Multiple 12 by 258, and then divide the answer by
2985.
49
Dr. Qurban Ali, EE Department
Exercise - 2
50
Dr. Qurban Ali, EE Department
Examples
AND AX, BX
◼ Let’s say AX = F324, BX = E122
◼ In binary we have: 1111 0011 0010 0100
AND 1110 0001 0010 0010
Result:1110 0001 0010 0000
AX = E120
51
Dr. Qurban Ali, EE Department
SAL/SHL Instructions
3 zeros shifted in
52
Dr. Qurban Ali, EE Department
SHR and SAR Instructions
Let AX = F000, CL = 3
◼ SAR AX, CL
◼ AX in binary is 1111 0000 0000 0000
◼ AX shifted by 3 1111 1110 0000 0000
AX = FE00
3 ones shifted in
Shift for multiplication
Example:
53
Dr. Qurban Ali, EE Department
Bit Scan Instructions
BSF scans the number from the leftmost bit toward the
right; BSR scans the number from the rightmost bit
toward the left.
◼ if a 1-bit is encountered, the zero flag is set and the bit
position number of the 1-bit is placed into the
destination operand
◼ if no 1-bit is encountered the zero flag is cleared
Examples:
For AX = 2F3D; CL = 02; CF = 1;
ROL AX, 1
RCR AX, CL
Exercise - 1
56
Dr. Qurban Ali, EE Department
Exercise - 2
57
Dr. Qurban Ali, EE Department
Exercise - 3
What is the result of executing following instructions?
MOV AL, A2H ; Initialize CF = 0;
CBW
CWD
XOR DX, AX
SAR DX, 3
RCR AX, 5
58
Dr. Qurban Ali, EE Department
Exercise - 4
59
Dr. Qurban Ali, EE Department