You are on page 1of 8

University of Alexandria

Faculty of Engineering
Division of Communications & Electronics

Subject Name: Microprocessors Lecturers: Dr. Nayera Sadek


Academic Year: 2016-2017 Assistant: Khaled Essam

Third Year – Semester 1

SHEET 4 Assembly language programming

1) a)What types of JMP are used when jumping within a segment?

Inter-segment jump

b) What is the difference between JMP DI and JMP [DI]?

JMP DI changes IP such that IP = DI


JMP [DI] changes IP such that IP = the word in memory pointed by DS:DI

c) Explain when the JA, JO and JCXZ instructions jump.

JA → Jump if above, jumps when the first operand is larger than the second operand, both are treated
as unsigned numbers
JO → Jump if overflow, jumps when OF = 1
JCXZ → Jump if CX = zero.

2) Multiply the 48 bit number 1F23FB670012H by 3.

MOV AX, 0012h


MOV BX, 0FB67h
MOV DX, 1F23h

MOV CX, AX
MOV SI, BX
MOV DI, DX

SHL AX, 1
RCL BX, 1
RCL DX, 1

ADD CX, AX
ADC BX, SI
ADC DX, DI

3) Explain why the following JMP instruction is SHORT jump:

CS: 100 MOV BL, 01H


CS: 102 MOV DX, 06754H
CS: 105 JMP 0109H
CS: 107 MOV BX, DX
CS: 109 NOP

What is the resulting machine code of the command JMP 0109H, given that the opcode of JMP is
EBH

The difference between 0109 h and 107h is 2h is in the range of -127 to +128, so it is a short jump

The machine code is EB02 h

4) What is the difference between the following programs?

Program 1 Program2
MOV AL,0FFH MOV AL,0FFH
MOV BL,01H MOV BL,01H
CMP AL,BL CMP AL,BL
JA AGAIN JG AGAIN
MOV DX,09H MOV DX,09H
JMP STOP JMP STOP
AGAIN:MOV DX,90H AGAIN:MOV DX,90H
STOP:NOP STOP:NOP

In program 1, AL is above BL so the code jumps to AGAIN, DX = 90


In program 2, AL is -1 and BL is 1, so AL is not greater than BL so the code continues MOV DX = 09

5) Develop a short sequence of instructions that perform the following operations:


a) Read two numbers (1 byte each) from port 10H. If first number > second number, add the two
numbers, otherwise subtract second from first. Store the final result in BL.

IN AL, 10 h
MOV BL, AL
IN AL, 10 h
CMP BL, AL
JG ADDNUM
SUB BL, AL
JMP EXIT
ADDNUM:
ADD BL, AL
EXIT:
HLT

b) Read two successive numbers in memory, then determine which is larger. Store the larger value
in the third memory location.

MOV AL, MEM


MOV AH, MEM+1
CMP AH, AL
JG FIRST
MOV AH, AL
FIRST:
MOV MEM+2, AH
EXIT: HLT

c) Read two numbers X and Y, where X is in AL and Y in AH. Determine the greater number such
that if (X > Y), calculate (X –Y), otherwise calculate (Y-X).
MOV AL, X
MOV AH, Y
CMP AL, AH
JG FIRST
SUB AH, AL
MOV AL, AH
JMP EXIT
FIRST:
SUB AL, AH
EXIT: HLT

6) Given an array A(I) with 100 8-bit signed integer numbers, write a program to generate a new
array B(I) as follows:
B(I) = A(I) for I = 1, 2, 99 and 100.
B(I) = the average of A(I-2), A(I-1), A(I), A(I+1) and A(I+2) for all others Is.

LEA SI, ARRAY1


LEA DI, ARRAY2
MOV CX, 100
Again:
LODSB
CMP CX, 3
JL Next
CMP CX, 98
JG Next
CALL AVERAGE
Next:
STOSB
LOOP Again
HLT

AVERAGE PROC NEAR


PUSH CX
MOV CL, 5
MOV AL, 0
ADD AL, [SI-3] ; A(I-2)
ADD AL, [SI-2] ; A(I-1)
ADD AL, [SI-1] ; A(I)
ADD AL, [SI] ; A(I+1)
ADD AL, [SI+1] ; A(I+2)
CBW
IDIV CL
POP CX
RET
AVERAGE ENDP

7) Describe the difference between a jump and call instructions.

CALL is an unconditional JMP to a process that pushes IP or IP and CS on the stack.

8) Develop a short sequence of instructions that perform the following operations:


a) Write a procedure that multiplies DI by SI and then divides the result by 100H. The result is
stored in AX upon returning from the procedure. Make sure that none of the other register or
flags change.

MyProc PROC NEAR


PUSHF
PUSH CX
PUSH DX

MOV AX, DI
IMUL SI
MOV CX, 100h
IDIV CX

POP DX
POP CX
POPF
RET
MyProc ENDP

b) Read a list of 5 signed numbers and determine whether each number is negative or positive. If
it is positive, do not do anything. If it is negative, call a procedure to get the absolute value of
that number. Store the absolute value in the same place of the negative number.

LEA SI, ARRAY


MOV CX, 5
Read:
LODSB
CMP AL, 0
JGE Next
CALL ABS
MOV [SI-1], AL
Next:
Loop Read
HLT

ABS PROC NEAR


PUSH BX
MOV BL, 0
SUB BL, AL
MOV AL, BL
POP BX
RET
ENDP ABS

9) How many interrupt vectors are available in the 8086/8088?

256

10) Explain what an INT 40 instruction does, making sure to indicate the memory location of this
vector.
Calls the interrupt vector routine at address 40x4 = 160 → A0 h
a) Pushes the flags (PUSHF)
b) Clears IF and TF
b) Pushes CS
c) Reads the new CS from [A2h] and [A3h]
d) Pushes IP
e) Reads the new IP from [A0h] and [A1h]
f) Jumps to CS:IP

11) How does the IRET instruction differ from RET?

Just as the far RET, it pops CS and IP from the stack. However, it also pops the flags from the stack
in order to restore the values of the IF and TF.

12) The following program defines INT 7(MULREG) as follows: AX=AX*BX-DX+CX. Trace the
program using AX=10H,BX=20H,DX=200H,CX=4FH to make sure that AX will be 4FH. If the
program contains any errors, correct it.

ORG 0001CH
DD MULREG
ORG 100h
MAIN PROC NEAR
MOV AX,10H
MOV BX,20H
MOV DX,200H
MOV CX,4FH
INT 7
HLT
MAIN ENDP

MULREG PROC NEAR


MUL BX
ADD AX,CX
SUB AX,DX
IRET
MULREG ENDP

ORG 0001CH
DD MULREG

ORG 100h
MAIN PROC NEAR
MOV AX,10H
MOV BX,20H
MOV DX,200H
MOV CX,4FH
INT 7
HLT
MAIN ENDP

MULREG PROC NEAR


MUL BX
DX-AX ← AX x BX = 16 x 32 = 512
DX = 0, AX = 512
ADD AX,CX
AX ← AX + CX = 512 + 79 = 591
SUB AX,DX
AX ← AX – DX = 591 – 512 = 79 = 4Fh
IRET
MULREG ENDP

So it does make AX = 4Fh. But it clears DX as a side effect. So we can use MUL BL instead.

13) The following program is assumed to add the value of AX and BX and saves the result in DX
without changing the values of AX or BX. Trace the program to make sure that it DOES NOT
perform the task correctly. Correct the program to do the required task.

ORG 100h
MAIN PROC NEAR
CS:100 MOV SP,100H
CS:103 MOV AX,20H
CS:106 MOV BX,30H
CS:109 MOV DX,3FH
CS:10C PUSH AX
CS:10D PUSH BX
CS:10E CALL ADDREG
CS:111 MOV DX,AX
CS:113 POP BX
CS:114 POP AX
CS:115 MOV CX,35H
CS:118 HLT
MAIN ENDP

ADDREG PROC NEAR


MOV DX,0113H
PUSH DX
ADD AX,BX
RET 2
ADDREG ENDP

ORG 100h
MAIN PROC NEAR
CS:100 MOV SP,100H
SP = 0100h
CS:103 MOV AX,20H
AX = 0020 h
CS:106 MOV BX,30H
BX = 0030 h
CS:109 MOV DX,3FH
DX = 003Fh
CS:10C PUSH AX
SP = 00FE h
[FE] = 20 h
[FF] = 00 h
CS:10D PUSH BX
SP = 00FC h
[FC] = 30h
[FD] = 00 h
CS:10E CALL ADDREG
SP = 00FA h
[FA] = 11 h
[FB] = 01 h
IP = ADDREG
CS:111 MOV DX, AX
DX = 0050 h
CS:113 POP BX
BX = 0030 h
SP = 00FE h
CS:114 POP AX
AX = 0020 h
SP = 0100 h
CS:115 MOV CX,35H
CX = 0035 h
CS:118HLT
MAIN ENDP

ADDREG PROC NEAR


MOV DX,0113H
DX = 0113 h
PUSH DX
SP = 00F8 h
[F8] = 13 h
[F9] = 01 h
ADD AX,BX
AX = 0020 h + 0030 h = 0050 h
RET 2
IP = 0113 h –> ERROR !!, it must return to the same stored IP which is 0111
SP = 00FC h
ADDREG ENDP

The correction is to replace RET 2 by


POP DX
RET

----------------------------------------------------------------------------------------------------------------------------------

List Of Conditional Jump

Opcode Condition Tested Function


JA/JNBE CF = 0 and ZF = 0 Jumps above/jumps not below or equal to
JAE/JNB CF = 0 Jumps above or equal to/jumps not below
JB/JNAE CF = 1 Jumps below/jumps not above or equal to
JBE/JNA CF = 1 or ZF = 1 Jumps below or equal to/jumps not above
JC CF = 1 Jumps carry set
JE/JZ ZF = 1 Jumps equal/ jumps 0
JG/JNLE OF = ZF and SF Jumps greater/jumps not less than or equal to
JGE/JNL SF = OF Jumps greater than or equal to/jumps not less than
JL/JNGE SF = OF Jumps less than/jumps not greater than or equal to
JLE/JNG Z = 1 or S = 0 Jumps less than or equal to/jumps not greater than
JNC CF = 0 Jumps no carry
JNE/JNZ ZF = 0 Jumps not equal to/jumps not 0
JNO OF = 0 Jumps no overflow
JNP/JPO PF = 0 Jumps no parity/jumps parity odd
JNS SF = 0 Jumps no sign (positive)
JO OF = 1 Jumps on overflow
JP/JPE PF = 1 Jump parity/jumps parity even
JS SF = 1 Jumps sign (negative)
JCXZ CX = 0 Jumps if CX = 0

You might also like