You are on page 1of 2

Home Work 7

Deadline: December 16, at 23:59


1. Problem
Consider the following MIPS assembler code. Assume that register a0 is used for the input and initially contains
n, a positive number. Assume that register v0 is used for the output.
Register zero is the so-called zero-register. It is a read-only register containing always 0. The instruction
slt $1,$2,$3 (set-on-less-than) assigns the value 1 to register $1 if $2 < $3 holds. Otherwise, $1 is set to 0.

begin: addi $s0,$zero,0


addi $s1,$zero,1
loop: slt $t0,$a0,$s1
bne $t0,$zero,finish
add $s0,$s0,$s1
addi $s1,$s1,2
j loop
finish: add $v0,$s0,$zero

a. Add comments to the every line of the program.


b. Give a C code segment that a compiler might have translated into this assembler code.
c. Describe in one sentence what the program computes.

2. Problem
a. Translate the following C-code fragment into a sequence of MIPS assembly language instructions. The
variables a, b, c are arrays of integer values. The integer-variable size contains the size of these 3 arrays.
Assume that the registers s0, s1, s2 contain the addresses of a, b, c and that register s3 and s4 contain n and
size (size ≥ 0).

for (int n=0; n<size; ++n)


{
c[n]=a[n]+b[n];
}

b. Your program is executed on a Computer operating at a clock frequency of 2 GHz. Executing one single
instruction takes the following number of clock cycles:
Instruction #clock cycles
add,addi,slt 4
lw,sw,beq,bne,j 5
How much time is required for executing your program assuming a value of 1000 for size?

3. Problem
Write a procedure convert in MIPS-Assembler language. This procedure converts a string consisting of
ascii-coded characters representing digits 0 – 9 followed by a NULL-character (one single byte with the value 0)
into an integer number. The first character of the string contains the most significant digit. The procedure expects
its address in Register a0. The trailing Null-character is used to mark the end of the string. The result shall be
placed in register v0. If the string contains other characters than digits the procedure shall return -1.
Remark:
For loading one single byte you may use the instruction lbu (load byte unsigned):
lbu $1,x($2) : copy the byte found in memory cell ($2 + x) into the lower 8 Bits of register $1 and set
the remaining bits of $1 to 0;
Logical shift operations may be performed by using the instructions ssl (shift left logical) and sslv (shift
left logical variable):

Page 1 of 2
ssl $1,$2,x : shift $2 left logically by x Digits and store result in $1
sslv $1,$2,$3 : shift $2 logically by $3 digits left and store result in $1

Page 2 of 2

You might also like