1. (a)
Express the following in 16-bit two¡¯s complement code, giving your answers in hexadecimal. Show your working.
i. 180
ii. -30
(b) Give the range of values that can be represented by a 32-bit two¡¯s complement code (express the range using powers of 2) and explain why.
[4]
[2]
[4]
[7]
[3]
(c) Find two 8-bit codewords, X and Y, such that X+Y overflows if the code being used is unsigned but does not if it is two¡¯s complement. Now find two codewords, W and Z, such that W+Z overflows in both codes. Explain your reasoning carefully.
(d) Write a Sigma16 program that takes an n-element array, X, of unsigned numbers and subtracts 1 from each element, except when the element is already 0. Assume that n is an unsigned variable in data memory indicating the number of elements in the array. Fully comment your code.
(e) Referring to your solution for (d), discuss what maximum value of n it would work for, explaining what factors you are considering.
For reference, here is part of the instruction set of the Sigma16 CPU.
lea
Rd, x[Ra]
Rd:= x +Ra
load
Rd, x[Ra]
Rd:= mem[x +Ra]
store
Rd, x[Ra]
mem[x +Ra]:=Rd
add
Rd,Ra,Rb
Rd:= Ra+Rb
sub
Rd,Ra,Rb
Rd:= Ra-Rb
mul
Rd,Ra,Rb
Rd:= Ra*Rb
div
Rd,Ra,Rb
Rd:= Ra/Rb, R15:=Ra mod Rb
and
Rd,Ra,Rb
Rd:= Ra AND Rb
inv
Rd,Ra,Rb
Rd:= NOT Ra
or
Rd,Ra,Rb
Rd:= Ra OR Rb
xor
Rd,Ra,Rb
Rd:= Ra XOR Rb
cmplt
Rd,Ra,Rb
Rd:= Ra
shiftl
Rd,Ra,Rb
Rd:=Ra logic shifted left Rb places
shiftr
Rd,Ra,Rb
Rd:=Ra logic shifted right Rb places
jumpf
Rd, x[Ra]
If Rd=0 then PC:=x+Ra
jumpt
Rd, x[Ra]
If Rd<>0 then PC:=x+Ra
jal
Rd, x[Ra]
Rd:= pc, pc: =x +Ra
trap
Rd,Ra,Rb
PC:= interrupt handler
jump
x[Ra]
PC:= x +Ra
CONTINUED OVERLEAF
Page 1
2 (a)
Distinguish between primary and secondary memory and explain briefly how the CPU accesses data stored in each. If a user were experiencing problems with the performance of a PC running a modern operating system using virtual memory, under what circumstances would you recommend increasing the amount of primary memory? Explain your reasoning.
[6]
(b) The following Sigma 16 code is intended to take an 8-element array (only first element is shown) and add all the elements placing the result in another variable, sum. However, although the code will assemble, it contains several errors.
i. Draw up a register-use table for the program (suitable for inclusion as comment).
ii. Identify the errors, in each case explain what is wrong, and how you would correct it.
iii. Write out the corrected program.
LEA R1,1[R0] ADD R2,R0,R0 LEA R3,n[R0] ADD R4,R0,R0
;Set R1 to constant 1 ;i:=0
;SetR3ton
;sum=0
;Is i