Good
Good
Good
mov bx,[bs+bp+200]
Two base registers are involved which is illegal. We can only add index register
with base.
mov ax, [bp+ si+100]
Q7) Calculating physical address.
1) Suppose CS register contains the value “1DDD” in hexadecimal and IP register contains
the following value “0436” again in hexadecimal.
2) The binary value of CS will be “0001110111011101”.
3) Append four binary zeros to the Least Significant Side, now it becomes:
“00011101110111010000”
4) The binary value of IP register is “0000010000110110”.
5) Append four binary bits to the Most Significant Side, now it becomes:
“00000000010000110110”
Compiled By : Muhammad Ishfaq.
6) Now the Segment Value is “00011101110111010000”
7) Offset Value is “00000000010000110110”
8) Add Both of these binary numbers and you get the actual physical address:
00011101110111010000+00000000010000110110= 00011110001000000110
9) So the actual physical address is “11110001000000110”.
How these instructions are legal or illegal. If illegal tell the reason and how we can
make this legal,
if legal then what these instructions do.
a. mov ax, bx
b. mov 10,ax
c. mov bx, [num2]
d. mov [num1],[num2]
e. add [num2],[num1]
f. mov [num1],al
Solution:
a. mov ax, bx
This instruction is legal because register to register move is allowed.
b. mov 10,ax
This instruction is illegal because the we cant move a register or memory address into a
constant. It can be rectify mov bx,ax or mov [var],ax.
c. mov bx, [num2]
This instruction is legal because memory to register is also allowed.
d. mov [num1],[num2]
This instruction is illegal because memory to memory is not allowed. It can be rectify
Mov ax, [num2]
Mov [num1], ax
e. add [num2],[num1]
This instruction is illegal because memory to memory operation is not allowed. It can be
rectify
mov ax,[num1]
add [num2], ax
f. mov [num1],al
This instruction is legal because register to memory is allowed.
Assemble the following code and step through it inside the debugger. Observer the
values of AX, BX, and the four words starting at “num1” after every instruction and fill
them in the table given below. All number entered in the table must be in hexadecimal.
Also give the reason why the result in [num1+6] is not 30 as expected.
[org 0x100]
mov ax, [num1]
mov bx, [num1+2]
add ax, bx
mov bx, [num1+4]
add ax, bx
mov [num1+6], ax
Calculate the effective address and the physical address accessed by each of the
following instructions. Also give the value of the designated register in each
instruction by looking up the dump of a portion of physical memory given below.
All questions are independent of each other. Assume the registers are initialized as
ES = 0x4020
DS = 0x40A0
BX = 0x0010
SI = 0x000E
BP = 0x0000
SS = 0x4010
If you calculate a physical address and it is not present in the table given above then simple
write that “Address is not given in table”.
Solution:
mov ax, [30]
First we have to calculate offset which is decimal 30 (0x1E).
Effective Address = 0x0030
Physical Address = (Segment Address * 0x10) + Effective Address
= (0x40A0 * 0x10) + 0x001E
= 0x40A1E
The Address is not present in the Memory Dump.
________________________________________________________________________
mov cx,[es:bx-0x10]
Effective Address = bx – 0x10
= 0x0010 – 0x10 = 0x0000
Physical Address = (Segment Address * 0x10) + Effective Address
= (0x4020 * 0x10) + 0x0000
= 0x40200
The Value of CX Register from the Memory Dump is = 0x0A43
mov bx,[bp + 0x0F]
Effective Address = bp + 0x0F
= 0x0000 + 0x000F = 0x000F
Compiled By : Muhammad Ishfaq.
Physical Address = (Segment Address * 0x10) + Effective Address
= (0x4010 * 0x10) + 0x000F
= 0x4010F
The Address is not present in the Memory Dump (One byte is missing).
mov cx, [0x30 + 0x15]
Effective Address = 0x45 = 0x0045
Physical Address = (Segment Address * 10) + Effective Address
= (0x40A0 * 0x10) + 0x0045
= 0x40A45
The Address is not present in the Memory Dump.
mov byte al,[0x0E]
Effective Address = 0x000E
Physical Address = (Segment Address * 0x10) + Effective Address
= (0x40A0 * 0x10) + 0x000E
= 0x40A0E
The Value of AL register from the Memory Dump is 75.
mov ax, [bx+si+0x15]
Effective Address = 0x0010 + 0x000E + 0x0015
= 0x0033
Physical Address = (Segment Address * 0x10) + Effective Address
= (0x40A0 * 0x10) + 0x0033
= 0x40A33
The Address is not present in the Memory Dump.
mov bx,[0x11]
Effective Address = 0x0011
Physical Address = (Segment Address * 0x10) + Effective Address
= (0x40A0 * 0x10) + 0x0011
= 0x40A11
The Address is not present in the Memory Dump.
Effective Address Physical Address Register Value
Mov ax, [30] 0x001E 0x40A1E Not given
Mov cx, [es:bx-0x10] 0x0000 0x40200 0x0A43
Mov bx, [bp+0x0F] 0x000F 0x4010F Byte not there
Mov cx, 0x0045 0x40A45 Not given
[0x30+0x15]
Mov byte al, [0x0E] 0x000E 0x40A0E 0x75
Mov ax, 0x0033 0x40A33 Not given
[bx+si+0x15]
Mov bx, [0x11] 0x0011 0x40A11 Not given
end2:
add bx, 2 ; advance bx to next index
loop top2
Suppose AL contains 10011011b and CF= 0. Give the new contents of AL after each of
the following instructions is executed. Assume the preceding initial conditions for
each part of this question.
a. SHL AL,1
b. SHR AL, CL if CL contains 3
c. ROL AL ,1
d. SAR AL, CL if CL contains 3
e. RCR AL,CL if CL contains 2
Solution:
a. AL=00110110 CF=1
b. AL=00010011 CF=0
c. AL=00110111 CF=1
d. AL=11110011 CF=0
e. AL=10100110 CF=1
Q#1 AX contains a number between 0-15. Write code to complement the corresponding
bit in BX. For example if AX contains 6; complement the 6th bit of BX. (Note: First bit
in BX is at 0th position and last bit is at 15th position)
Solution:
[org 0x0100]
mov ax,3 ;suppose ax contain 3
mov ax,4ch
int 21h
Q#2 Suppose that AX=0x3412, BX=0x7856, CX= 0x1CAB, and SP=0x100. Given the
contents of AX, BX, CX, and SP after executing the following instructions:
PUSH AX
PUSH BX
XCHG AX, CX
POP CX
PUSH AX
POP BX
SOLUTION:
AX BX CX SP
PUSH AX 0x3412 0x7856 0x1CAB 0xFE
PUSH BX 0x3412 0x7856 0x1CAB 0XFC
XCHG 0x1CAB 0x7856 0x3412 0XFC
AX,CX
POP CX 0x1CAB 0x7856 0x7856 0xFE
PUSH AX 0x1CAB 0x7856 0x7856 0XFC
POP BX 0x1CAB 0x1CAB 0x7856 0xFE
Identify the problems in the following instructions and correct them by replacing them
with one or two instruction having the same effect.
Write a program to calculate the square of 10 by using a loop that adds 10 to the
accumulator 10 times.
Solution:
[org 0x0100]
mov bx,10
mov cx,10
mov ax,0
l1:
add ax, bx
sub cx, 1
jnz l1
mov [total], ax
mov ax,0x4c00
int 0x21
total: dw 0
If AX=8FFF and BX=0FFF and “cmp ax, bx” is executed, which of the following jumps
will be taken? Each part is independent of others. Also give the value of Z, S, and C
flags.
i. jg greater
ii. jl smaller
iii. ja above
iv. jb below
Instructions Jump ZF SF CF
Jg greater Not taken 0 1 0
Jl smaller Taken 0 1 0
Ja above Taken 0 1 0
Jb below Not taken 0 1 0
a. As an application of the shift and rotate instruction, let’s consider the problem of
reversing the bit pattern in a byte or word. For example, if AL contains 11100100,
we want to make it 00100111. Write the code that reverses the bit pattern in AL
register.
b. Show the values of each register which you have used in your program after the
each iteration of shifting or rotating bits with the following pattern.
REV:
SHL AL, 1
RCR BL, 1
LOOP REV
MOV AL, BL
mov ax,1001100110011001b
mov bx,0
mov dx, 0 ; initialization
mov bx, ax ; get a copy of ax
and bx, 0011001100110011b ; bx now has 01, 45, 89, 1213 bits
rol bx, 2
mov cx, 0
mov cx, bx
or cx, dx ; ORing of bx and dx will produce desired result
0000:6727
Physical Address = Segment x10 + Offset
Physical Address = 0000 x10 + 6727
Physical Address = 00000 + 6727
Physical Address = 6727h
FFFF:4336
Physical Address = Segment x10 + Offset
Physical Address = FFFF x10 + 4336
Physical Address = FFFF0 + 4336
Physical Address = 104326h
To make 20bit physical address we have to drop the carry bit so
Physical Address = 04326h
AB01:FFFF
Physical Address = Segment x10 + Offset
Physical Address = AB01 x10 + FFFF
Physical Address = AB010 + FFFF
Physical Address = BB00Fh
0FFF:FFFF
Physical Address = Segment x10 + Offset
Physical Address = 0FFF x10 + FFFF
Physical Address = 0FFF0 + FFFF
Physical Address = 1FFEFh
b. Identify the problems in the following instructions and correct them by replacing them
with one or two instruction having the same effect.
Compiled By : Muhammad Ishfaq.
x. mov [02], [ 22]
; Memory to memory move is illegal in Intel architecture. The correct instructions
for this operation could be
mov ax, [22]
mov [22],ax
xiv.mov bx,[bs+bp+200]
; Addition of two based register in one memory access is disallowed. We can do
this operation by addition of index and base register.
mov ax,[bp+ si+100]
a. Write a program in assembly language that calculate the factorial of a number where
factorial is defined as:
Factorial(x) = x*(x-1)*(x-2)*...*1
factorial (0) = 1
[org 0x0100]
mov ax, 1 ; ax hold the factorial
mov cx, 5 ; suppose cx contain the value for which factorial is to
; determined.
top:
mul cx ; ax = ax * cx
loop top
b. If AX=8FFF and BX=0FFF and “cmp ax, bx” is executed, which of the following
jumps will be taken? Each part is independent of others. Also give the value of Z, S,
and C flags.
a) jg greater
Compiled By : Muhammad Ishfaq.
b) jl smaller
c) ja above
d) jb below
Instructions Jump ZF SF CF
Jg greater Not taken 0 1 0
Jl smaller Taken 0 1 0
Ja above Taken 0 1 0
Jb below Not taken 0 1 0
a. What is the difference between shift right (SHR) and shit arithmetic right (SAR)
instructions?
The SAR instruction operates like SHR, with one difference, the msb retains its original
value.
b. What is the difference between AND and TEST instructions?
The TEST instruction perform an AND operation of the destination with the source but
does not change the destination contents
c. The relations of CALL and RET instructions with PUSH and POP instructions.
When CALL instruction is executed the PUSH operation is to be perfumed to save the
address of next instruction and when the RET instruction is executed the POP
operations is to be performed to get the return address of next instruction to be
executed
Write a sequence of instruction to do each of the following:
a. Put the sum 1+ 4+7+ … +148 in AX
Solution:
MOV AX, 0 ; initialize AX
MOV BX, 1 ; contain every added value
WHILE_:
CMP BX, 148 ; compare with last vale
JG ENDWHILE_
ENDWHILE_:
b. Put the sum 100+95+90+ … +5 in AX
Solution:
MOV AX, 0 ; initialize AX
MOV BX, 100 ; contain every adding value
WHILE_:
CMP BX, 5 ; compare with last vale
JL ENDWHILE_
ENDWHILE_:
Compiled By : Muhammad Ishfaq.
1. Identify the problems in the following instructions and correct them by replacing
them with one or two instruction having the same effect.
a. mov [02], [22]
b. mov [wordvar], 20
c. mov bx, al
d. mov ax, [si+di+100]
Solution:
a)
mov ax, [22]
mov [02], ax
OR
mov al, [22]
mov [02], [al]
b)
mov al, 22
mov [wordvar], al
c)
mov bl, al
d)
Mov bx, si
Mov ax, [bx+di+100]
Solution:
a. ret
b. jmp L5
c. push ax
d. pop ax
e. call
As an application of the shift and rotate instruction, let’s consider the problem of
reversing the bit pattern in a byte or word. For example, if AL contains 11011100, we
want to make it 00111011. Write the code that reverses the bit pattern in AL register.
Solution:
MOV AL, 11011100b
MOV BL,0
MOV CX, 8
REV:
SHL AL, 1
RCR BL, 1
LOOP REV
MOV AL, BL
MOV AX, 0x4c00
INT 0x21
Consider the given block of code from an assembly language program. And suppose
that AX= 1234h, BX= 5678h, CX= 9ABCh and SP= 100h.
Give the contents of AX, BX, CX and SP after executing the following block of
instructions.
PUSH AX
PUSH BX
XCHG AX, CX
POP CX
PUSH AX
POP BX
Compiled By : Muhammad Ishfaq.
VALUES AFTER THE EXECUTION OF INSTRUCTION
Instructions AX BX CX SP Stack Top
--- 0x1234 0x5678 0x9ABC 0x0100 ---
PUSH AX 0x1234 0x5678 0x9ABC 0x00FE 0x1234
PUSH BX 0x1234 0x5678 0x9ABC 0x00FC 0x5678
XCHG AX, CX 0x9ABC 0x5678 0x1234 0x00FC 0x5678
POP CX 0x9ABC 0x5678 0x5678 0x00FE 0x1234
PUSH AX 0x9ABC 0x5678 0x5678 0x00FC 0x9ABC
POP BX 0x9ABC 0x9ABC 0x5678 0x00FE 0x1234
SO THE FINAL VALUES ARE
0x9ABC 0x9ABC 0x5678 0x00FE 0x1234h