Sie sind auf Seite 1von 10

24-02-2007

BILKENT UNIVERSITY
Electrical and Electronics Engineering Dept.
EEE212 Microprocessors
SECTIONS 1-5 Midterm
Closed Book No Calculators
All programs need to be in assembly language!
Use the boxes provided for your answers!
Duration: 2 hours

Surname:_______________________________________

Name:_________________________________________

ID-Number:_____________ SECTION: ___________

Signature:_______________________________________

Q1 (25 points)
Q2 (25 points)
Q3 (25 points)

Q4 (25 points)
TOTAL
Q1) [25 pts] a) [3 pts] Represent -2668 decimal in 32 bit signed arithmetic as a hex value

-2668 = FFFFF594 hex

b) [3 pts] What is 8DCE h in decimal if we are using 16 bit signed arithmetic

8DCE h = -29234 decimal

c) [7 pts] Consider the following program. At ROM address 07FFh, we are to jump to HERE1 or
HERE2 or HERE3. For each of the three cases, show which type of jump instructions (LJMP,
SJMP, or AJMP) can be used and which one(s) is to be preferred? Explain. Hint. AJMP uses
absolute addressing as in ACALL.

ROM address hex Assembly Code Comment


07FF ? ; jump reqired to
HERE1-2-3

0E12 HERE1: MOV A,00h

07B2 HERE2: MOV A,01h

4000 HERE3: MOV A,02h

Write your answers in the table below.

Jump to Location What is What is (are) Explanation for what is possible and what
possible preferred is preferred
HERE1 AJMP,LJMP AJMP Takes less code space, 2 bytes vs 3 bytes

HERE2 SJMP,LJMP SJMP Takes less code space 2 bytes vs 3 bytes

HERE3 LJMP LJMP


d) [12 pts] Write an 8051 assembly language procedure to calculate round(x,y) which is defined as
the integer closest to x/y when x and y are unbsigned binary and y 0 and y< 0Ah. If there are two
such integers, the greater one will be selected. For example, let x=64h and y=06h then round(x,y)
= 11h (Observe that 100/6 = 16.666 17 after rounding) . We assume x and y are loaded into R3
and R4, respectively, before the call and the procedure will return round(x,y) in R5. Hint. After
division, you need to compare a function of the remainder and the denominator to find out whether
we need to round to the larger integer.
MUL AB

DIV AB

SUBB The subtract with borrow instruction subtracts the byte variable and the contents of the carry
from the accumulator, placing the result in the accumulator. The carry flag serves as a "Borrow
Required" flag during the subtraction; when a greater value is subtracted from a lesser value (eg, 1 - 6),
this requires a borrow, so the carry flag is set. On all other conditions, the carry flag is cleared.

SUBB A, R0 ;register, A = A - R0 - C
SUBB A, #23h
SUBB A, @R1
SUBB A, P0

ROUND: ROUND: MOV A,R3


MOV B,R4
DIV AB
MOV R5,A ; quotient is now in R5
MOV A,#02h
MUL AB ;2*remainder is now in A
CLR C
SUBB A,R4
JNC SKIP
INC R5
SKIP: RET
Q2) [25 pts]
a) [12 pts] For the following assembly program, determine the final values of R2, R3, R4 and P1 (all in
hexadecimal).

ORG 0H
CLR PSW.3
CLR PSW.4
MOV 40H, #0D9H
MOV 41H, #9AH
MOV 42H, #12H
MOV R0, #40H
MOV R2, #3
MOV R3, #29
CLR A

AGAIN: ADD A, @R0


JC HERE
INC R0
HERE: XRL 3, #0FH
DJNZ R2, AGAIN
ADD A, R2
MOV R4, A
LCALL COMP
SJMP $

COMP: CJNE R4, #33H, NOT_EQ


MOV P1, # E ; Letter E has ASCII code of 45H
SJMP LAST
NOT_EQ: JC SMALL
MOV P1, # L ; Letter L has ASCII code of 4CH
SJMP LAST
SMALL: MOV P1, # S ; Letter S has ASCII code of 53H
LAST: RET
END

Answer: R2= R3= R4= P1= (in hex)


b) [13 pts] Assume that there are 48 byte values at the 8051 RAM memory locations 50H, 51H,,7FH.
Write an assembly language procedure named FNDMX to find the value and the address of the maximum
of those 48 values. Store the maximum value in register R5 and the RAM address corresponding to the
maximum value in register R6. Consider all 48 byte values as unsigned numbers, and do NOT use the
CJNE instruction.
[Hint: For CY=0, SUBB A, @R0 instruction sets CY to 1 if the value stored at RAM address R0 is larger
than A; otherwise CY is cleared (CY represents the carry flag C).]
Q3) [25 pts] For the following code segments and the hardware connections, determine the
contents of the RAM given below after the execution of all the instructions. Note that empty bit-or
byte locations will be assumed as zero. Also determine the number displayed on the 7-segment
display. The 7-segment display is connected to P2 in the following configuration.

ORG 0H;
INTERNAL RAM BIT ADDRESSABLE RAM
MOV P1,#0FFH;
MOV A, P1; 00 00
PUSH 0E0H; 08 08
10 10
MOV R0, #11H; 18 18
MOV A, R0; 20 20
28 28
PUSH 0E0H; 30 30
SETB RS1; 38 38
POP 10H; 40 40
48 48
POP 11H; 50 50
MOV A,@R0; 58 58
60 60
CPL A; 68 68
ANL P1,#55H; 70 70
MOV P2,A; 78 78
...
MOV SP, #2FH; 90
PUSH 0E0H; A0
E0
PUSH 0A0H;
PUSH 90H;
P1, P2, A have RAM addresses of 90H, A0H, and E0H
MOV C,ACC.0; respectively.
MOV 00H,C;
+5V
MOV C,ACC.2; 74LS244

MOV 01H,C; ENB

MOV C,ACC.7;
MOV 02H,C;

JNC NUM;
CPL A;
ANL A,#7FH;
NUM: MOV P2,A;

SJMP $;

A B C D E F G DP

GND

Number on the display:

Q5) [25 pts] Please read this information carefully before starting the question.
Macro definitions: (extensions to instruction sets)

Macros are useful for code that is used repetitively throughout the program. It saves the
programmer the time and tedium of having to specify the code every time it is used.
The code is written only once in the macro definition and it can be used anywhere in the source
program any number of times by simply using the macro name.

Sometimes there is confusion between macros and subroutines. Subroutines are common routines
that are written once by the programmer and then accessed by calling them. Subroutines are
usually used for longer and more complex routines where the call/return overhead can be tolerated.
Macros are commonly used for simpler routines or where the speed of the inline code is required.

Macro is defined as

macro_name macro par1, par2,


set of instructions
set of instructions
endm

and in the program it is used as


macro_name par1, par2,

for example to switch between the register banks, it is common to use a macro. Both programs in
the columns of the table are same.

bank0 macro org 0


clr psw.3 setb psw.3
clr psw.4 clr psw.4
endm
mov r1, #5h
bank1 macro mov r2, #0Ah
setb psw.3
clr psw.4 clr psw.3
endm clr psw.4

org 0 mov r1, #1h

bank1 setb psw.3


mov r1, #5h clr psw.4
mov r2, #0Ah end
bank0
mov r1, #1h
bank1
end

Macros can be used with parameters, the assembler replaces the parameters with the arguments
when macro is used.
mymov macro source, destination org 0
mov destination, source mov A, #5
endm mov r1, #30h
again : mov @r1, A
goto macro label1 inc r1
sjmp label1 sjmp again
endm end

org 0

mymov #5,A
mymov #30h,r1
again: mymov A, @r1
inc r1
goto again

end

a) [8pts] Find the values of the registers after the execution of this program.
aoi22 macro a1,a2,b1,b2,z Answer :
mov A,a1
anl A,a2 R1 =
mov z,A
mov A,b1 R2 =
anl A,b2
orl A,z R3 =
cpl A
mov z,A R4 =
endm
R5 =
org 0
mov r1, #05h
mov r2, #0Ah
mov r3, #50h
mov r4, #0A0h
aoi22 r1,r2,r3,r4,r5
nop
aoi22 r1,r1,r4,r4,r3
nop
aoi22 r1,r1,r4,r4,r4
sjmp $
end
b) [9 pts] Write jg, jl, and je (use subb instruction for these macros, use registers A and B as
temporary if needed) and exchange macro (use registers A and B as temporary registers)
according to the descriptions

SUBB The subtract with borrow instruction subtracts the byte variable and the contents of the carry
from the accumulator, placing the result in the accumulator. The carry flag serves as a "Borrow
Required" flag during the subtraction; when a greater value is subtracted from a lesser value (eg, 1 - 6),
this requires a borrow, so the carry flag is set. On all other conditions, the carry flag is cleared.

SUBB A, R0 ;register, A = A - R0 - C
SUBB A, #23h
SUBB A, @R1
SUBB A, P0

Code to define the macro Descriptions of the macro


jg macro a1,a2,label1 if a1 is greater than a2 (a1 > a2) then jump to
label1

endm
jl macro a1,a2,label2 if a1 is less than a2 (a1 < a2) then jump to
label2

endm
je macro a1,a2,label3 if a1 is equal to a2 then jump to label3

endm
exchange macro a1,a2 Change the values of the parameters
a1 -> temp
a2 -> a1
temp -> a2

We do not have
mov r1,r2 or mov @r1,@r2
instructions so
endm Use two temporary registers (A, B)

c) [8 pts] Complete the following program using your macros in part b


in order to perform an inefficient bubble sort algorithm:
i- compare two consecutive numbers in a list, a(n) and a(n+1)
ii- if a(n) is greater then a(n+1) swap the numbers
iii - continue with the next two consecutive numbers in the list

when you reach the end of the list it means the that the largest number is at the end.
If you loop the list will be sorted.

This program moves the values 10 to 3 into


; assume your macros come here RAM locations 40h to 47h.
org 0
mov 40h, #0ah
mov 41h, #09h We want to sort the list
mov 42h, #08h
mov 43h, #07h
mov 44h, #06h
mov 45h, #05h
mov 46h, #05h
mov 47h, #03h
mov r7, #7h
outter:mov r6, #7h
mov r0, #40h
mov r1, #41h
inner : nop
; insert your code here

djnz r6, inner


djnz r7, outter
sjmp $

end

Das könnte Ihnen auch gefallen