Professional Documents
Culture Documents
Solutions - Midterm
Solutions - Midterm
(5 pts) Complete the following table. The numbers are represented with 8 bits.
REPRESENTATION
Decimal 1's complement 2's complement
-50 11001101 11001110
77 01001101 01001101
(5 pts) Perform the following addition and subtraction of 8-bit unsigned integers. Indicate (every carry) or borrow from c0
to c8 (or b0 to b8). For the addition, determine whether there is an overflow. For the subtraction, determine whether we
need to keep borrowing from a higher byte.
Example:
54 + 210 77 - 194
b8=1
b7=0
b6=0
b5=0
b4=0
b3=0
b2=1
b1=0
b0=0
c8=1
c7=1
c6=1
c5=1
c4=0
c3=1
c2=1
c1=0
c0=0
Borrow out!
54 = 0x36 = 0 0 1 1 0 1 1 0 + 77 = 0x4D = 0 1 0 0 1 1 0 1 -
210 = 0xD2 = 1 1 0 1 0 0 1 0 194 = 0xC2 = 1 1 0 0 0 0 1 0
Overflow! 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1
86 + 181 86 - 181
b8=1
b7=0
b6=1
b5=0
b4=0
b3=0
b2=0
b1=1
b0=0
c8=1
c7=1
c6=1
c5=1
c4=0
c3=1
c2=0
c1=0
c0=0
Borrow out!
86 = 0x56 = 0 1 0 1 0 1 1 0 + 86 = 0x56 = 0 1 0 1 0 1 1 0 -
181 = 0xB5 = 1 0 1 1 0 1 0 1 181 = 0xB5 = 1 0 1 1 0 1 0 1
Overflow! 1 0 0 0 0 1 0 1 1 1 0 1 0 0 0 0 1
(5 pts) Perform the following operations using the 2’s complement representation with 8 bits. Determine whether the
operations result in an overflow.
-59 - 114 -86 + 114
c8c7=1 c8c7=0
c2=0
c6=0
c5=0
c4=1
c3=1
c1=0
c0=0
c6=1
c5=0
c4=0
c3=0
c2=1
c1=0
c0=0
c7=1
c8=1
c7=0
c8=1
Overflow! No Overflow
-59 = 0xC5 = 1 1 0 0 0 1 0 1 + -86 = 0xAA = 1 0 1 0 1 0 1 0 +
-114 = 0x8E = 1 0 0 0 1 1 1 0 114 = 0x72 = 0 1 1 1 0 0 1 0
0x53 = 0 1 0 1 0 0 1 1 28 = 0x1C = 0 0 0 1 1 1 0 0
The range 0xD800 to 0xDFFF is akin to all possible cases with 11 bits. Thus the SRAM size is 2 11 bytes = 1 KB.
We only need 11 bits for this SRAM.
Address
1101 1000 0000 0000: 0xD800 $00
1101 1000 0000 0001: 0xD801 $00
...
...
...
1101 1111 1111 1111: 0xDFFF $00
Addressing Mode
clra
Inherent
clrb
Immediate ldx #$FADE
ldy #$1A00
A $00 B $00 X $FADE Y $1A00
Immediate, Indexed - Post-Increment movw #$1E20,1,Y+
A $00 B $00 X $FADE Y $1A01
Immediate ldab #$81
A $00 B $81 X $FADE Y $1A01
Inherent
sex b,d
A $FF B $81 X $FADE Y $1A01
Indexed - Pre-Decrement
addd 1,-Y
A $1D B $A1 X $FADE Y $1A00
Inherent
exg x,y
A $1D B $A1 X $1A00 Y $FADE
Indexed Indirect - 16 bit Offset
std [0,X]
0x1A00 $1E
0x1A01 $20
...
Determine whether the following statements are True or False. If the statement is false, explain why.
Inside an Interrupt Service Routine, the values of the PC and CPU registers are pushed in the Stack.
FALSE. The ISR does not do this. The processor does this before the ISR is executed.
TRUE
When servicing a Reset, the values of the PC and CPU Registers are pushed in the Stack.
FALSE. The processor does not save these registers, as the Reset will initialize these values.
Complete:
To enable/disable all maskable Interrupts, we configure the bit _I__ of CCR.
anda #$3E
lsla
lsla
(5 pts) What is the time delay (in ms) that the following loop generates? Assume a 25 MHz bus clock. Consider that pusha
takes 2 cycles, pula 3 cycles, nop one cycle and dbne 3 cycles.
ldx #56000
loop: nop ; 1 cycle
nop ; 1 cycle
psha ; 2 cycles
pula ; 3 cycles
psha ; 2 cycles
pula ; 3 cycles
dbne X, loop ; 3 cycles
𝑛𝑡𝑖𝑚𝑒𝑠 = 56000, 𝑛 = 15
1 1 33.6
𝑛 × 𝑛𝑡𝑖𝑚𝑒𝑠 × = 15 × 56000 × =
25 × 106 25 × 106 103
(10 pts) After the addd $10A0 instruction, what is the state of D and the following CCR flags: Z, C, V, and N? Does the bcs
next instruction branches to ‘next’? Yes or no? Why?
V = c16c15=1
c11=0
c14=0
c13=0
c12=0
c10=1
c16=0
c15=1
c6=0
c1=0
c9=1
c8=0
c7=0
c5=0
c4=1
c3=0
c2=0
c0=0
C = c16 = 0
0x730B = 0 1 1 1 0 0 1 1 0 0 0 0 1 0 1 1 +
0x41AC = 0 1 0 0 0 0 0 1 1 0 1 0 1 1 0 0
0xB4B7 = 1 0 1 1 0 1 0 0 1 0 1 1 0 1 1 1
S X H I N Z V C
D $B4B7 CCR 1 0 1 0
bcs: branch if carry set. Since C = 0, then bcs next DOES NOT branch to ‘next’.
... ...
0x1000 0x1000
0x1001 0x1001
...
...
ROMStart EQU $4000
0x3FF7 SP
; code section
ORG ROMStart
$BE
$BA Entry:
$40 _Startup: LDS #$4000
$11 myloop: movw #$FE,2,-SP
$CA movb #$CA,1,-SP
$00
ldd #$BEBA
0x3FFF $FE bsr myfun 0x3FFF
0x4000 0x4000 SP
0x4001 leas 3,SP 0x4001
... ...
adda #$80
...
...
0x400F bvs myloop 0x400F
; Subroutine
myfun: pshb
psha
leas -2,SP;
SP: $3FFD SP: $3FF7
movw #$FAD, SP
... leas 2,SP; ...
0x1000 pula 0x1000
0x1001 pulb 0x1001
rts
...
...
...
0x400F 0x400F
0x4011 0x4011