Professional Documents
Culture Documents
Question 1)
a) Translate the following C code into MIPS. Please comment your code.
Assume x is $s0; y is $s1; A is $a0 and points to integers; B is $a1 and points to
unsigned char.
You will lose points for assembler syntax errors.
1) x = (y > 7)? x + 2 : x 3;
addi$t0,$zero,7
slt$t1,$t0,$s1#if((3<y)==0)gotoL1
beq$t1,$zero,L1
addi$s0,$s0,2#then
jL2
L1:addi$s0,$s0,3#else
L2:
addi$t0,$s1,5
add$t0,$s0,$t1
andi$s0,$t0,0xFF
addi$t0,$s0,2#x+2
add$t1,$a1,$t0#(B+x+2)
lbu$t2,0($t1)#=*(B+x+2)
ori$t3,$t2,0x10
addi$t4,$s0,3#x+3
add$t5,$t4,$t4#2*(x+3)
add$t6,$t5,$t5#4*(x+3)
add$t7,$a0,$t6#A+x+3
sw$t3,0($t7)#*(A+x+3)=$t3
b) MIPS has several addressing modes, list all types of these addressing modes.
Question 2)
a) Write a procedure, bfind, in MIPS assembly language. The procedure should take a
single argument that is a pointer to a null-terminated string in register $a0. The bfind
procedure should locate the first b character in the string and return its address in
register $v0. If there are no bs in the string, then bfind should return a pointer to the
null character at the end of the string. For example, if the argument to bfind points to
the string rabbit, then the return value will be a pointer to the third character of the
string.
.data bfind:
string:.asciiz"hello" loop:
.text lb$t0,($a0)
.globlmain beq$t0,98,find
beq$t0,$zero,nullisfound
main: add$a0,$a0,1
la$a0,string bloop
jalbfind find:
move$v0,$a0
move$a0,$v0 bend
li$v0,1 nullisfound:
syscall move$v0,$a0
li$v0,10 end:
syscall jr$ra
b) How can you modify the answer of part a, so the program will count the number of b
characters in the string, and store their corresponding addresses into an array.
(Show modifications, or rewrite the program)
Question 3)
The following program tries to copy words from the address in register $a0 to the
address in register $a1, counting the number of words copied in register $v0. The
program stops copying when it finds a word equal to 0. You do not have to preserve the
contents of registers $v1, $a0, and $a1. This terminating word should be copied but not
counted.
loop: lw $v1,0($a0)
addi $v0,$v0,1
sw $v1,0($a1)
addi $a0,$a0,1
addi $a1,$a1,1
bne $v1,$zero,loop
There are multiple bugs in the MIPS program; fix them and turn in a bug-free version.
(Show changes clearly)
addi$v0,$zero,1#Initializetoavoidcountingzeroword
loop:lw$v1,0($a0)#Readnextwordfromsource
addi$v0,$v0,1#Incrementcountwordscopied
sw$v1,0($a1)#Writetodestination
addi$a0,$a0,4#Advancepointertonextsource
addi$a1,$a1,4#Advancepointertonextdest
bne$v1,$zero,loop#Loopifthewordcopied!=zero
Bugs:
1.Count($v0)isnotinitialized.
2.Zerowordiscounted.(1and2fixedbyinitializing
$v0to1).
3.Sourcepointer($a0)incrementedby1,not4.
4.Destinationpointer($a1)incrementedby1,not4.
Question 4)
A flow-control instruction is one that affects the program sequence, i.e., it changes the
contents of the Program Counter register. Examples of flow-control programming
constructs include conditional and "goto" statements. Give one instance of a flow-control
instruction for each of the three MIPS instruction formats (R, I and J). Provide a rank that
shows the order of each type in terms of the range it can address (small to large). Mention
how many bits of each instruction are reserved for the destination address. Based on the
instruction format, give reasons for such bit limitations.
Order by Number of
Instruction Addressing Complete Instruction Bits in the
Drawing and Reasons
Type Range (put Example Addressing
1, 2 or 3)* Field
Only 5 bits are used because only 32
R-type JR $ra
Instruction 1 Or JALR $ra 5 (32) registers are available to keep the
absolute address jumped to. (25=32)
Here the only operand is the label
J-type J label
Instruction 2 Or JAL label 26 (address) so we need 6 bits for the
opcode and 26 are left as an address
We need a 6b opcode, a 5b $S1, a5b
I-type BEQ $S1, $S2, label
Instruction 3 Or BNE $S1, $S2, label 16 $S2 and the remaining 16b are for
the address.
* 1 for the least range and 3 for the highest range.
Question 5)
After loading the assembly program shown below into PCSPIM we get the figure shown
below, according to the program code and the figure below, answer the following
questions.
main:
la $a0,string2
jal bfind
.data
string : .asciiz "hello"
string2: .asciiz "Find me\n"
Question 5)
Modify the 1-bit ALU covered in class (supporting AND, OR, +, and -) to implement
an XOR (exclusive OR) operation. Draw the entire ALU, adding the fewest possible
gates. Use the available op code 11 to select the XOR operation.
(NOTE: you have to construct the solution without using an XOR gate).
Good Luck
Ans:
Overflow or underflow or exception (accept any one of the 3)
B0 A3 B2 A2 B1 A1 B0 A0
FA FA FA FA
C4 C3 C3 C2 C2 C1 C1 C0
S0 S0 S0 S0
Ans:
Question 4
Suppose that you have already written a MIPS function with the following
signature:
This function calculates the sum of the elements of A starting with element first and
ending with element last. Write a fragment of MIPS assembly language which calls
this function and uses it to calculate the average of all values in A. You may assume
that the size of the array A is N.
Ans:
la $a0, A # First parameter is address of array element 0
lw $a1, $zero # Second parameter: index of elem at low end of range
lw $a2, N # Third parameter: index of last elem to sum
addi $a2, $a2, -1 # N was size: index of last element is N-1
jal sum # Call the sum function
move $t0, $v0 # Save the return value in $t0
lw $t1, N # Load size of array into $t1
div $t2, $t0, $t1 # This form of div is provided as a pseudoinstruction.
Question Twelve
assume that the contents of registers and memory is as indicated below. You should tell
all the changes to registers and memory caused by the given instruction. For instance, an
answer might be, "After the instruction is executed, register 6 will contain 25, and
memory locations 44 to 47 contain 20.
Treat each question separtately, that is, the memory and registers always start as shown
below.
Registers (This shows the contents of the first 8 registers. The values are expressed in
decimal.)
0 8 28 16 44 36 20 20
Reg 0 Reg 1 Reg 2 Reg 3 Reg 4 Reg 5 Reg 6 Reg 7
Memory (This shows the contents of the first 64 Bytes of main memory, where each 4-
Byte word is shown as a decimal number.)
20 16 12 8 4 0 24 32
Bytes 0 to 3 4 to 7 8 to 11 12 to 15 16 to 19 20 to 23 24 to 27 28 to 31
3148 24 0 4 44 17 16 12
Bytes 32 to 35 36 to 39 40 to 43 44 to 47 48 to 51 52 to 55 56 to 59 60 to 63
1. For the MIPS assembly instruction sw $4,8($2),
(a) Write the machine instruction in binary (yes, I want you to write out 32 0's and
1's.) The charts inside the back cover of the textbook should help. Please note that
the order of the operands in assembly code is different from the order in machine
code.
(b) Tell how registers and/or memory would be changed.
2. For the MIPS instruction add $2,$0,$1,
(a) Write the instruction in binary, and
(b) Tell how registers and/or memory would be changed.
Briefly discuss the difference between Big Endian and Little Endian
2. Why are the operands for arithmetic instructions sign-extended, but not so those for logical
instructions.
Convenience the range -32K to 32K is more versatile than 0 to 64K this eliminates the need
for a subi instruction.
3. The pseudoinstruction la rd, imm (load address) is assembled into a lui (load upper
immediate) instruction followed by an ori (or immediate) instruction.
Address constants are usually larger than 16 bits, so you need to load a 32-bit constant.
You want to set both halves of the word, and lui zeroes the lower half, thus undoing your work.
Ori lets the top half of the word alone, so you can set first top, then bottom.
addi causes sign extension, and give the wrong result if its argument is negative.
7. Assume a move rs, imm pseudoinstruction exists. It is similar but not identical to the la
instruction. Explain the difference between it and the la.
la load effective address loads an address, which the assembler calculates at assemble time (not
run) by looking at its symbol table (determined by the .data segment of the program). The move
or li is the same except it uses the numerical value of the constant rather than the looked-up
value.
Part (a) (2 marks) Suppose in a MIPS processor that $s0contains 0xb000_0000, $s1
contains
0x9a00_0000and that the instruction
addu$s2,$s0,$s1
is executed. What bit pattern does the instruction leave in $s1? (To get full marks you must show
how
you obtained your answer.)
Part b. (3 marks) Briefly define the term overflow as it is applied to integer addition. Did
overflow
occur in the instruction of part (a)? Show how you decided whether overflow occurred.
Part c. (3 marks) Briefly define the term wraparound as it is applied to integer addition. Did
wraparound
occur in the instruction of part (a)? Show how you decided whether wraparound occurred.