# Spring 06 CS61C Homework 6

• P&H: 2.10, A.1-A.4 (On CD) or online

# Problems

## Question 1

Some instruction set architectures offer an "exchange" instruction which exchange the values of two registers. We want to support such exchange instruction in MIPS by introducing a new MAL instruction:

```exch \$Rs \$Rt
```

Implement this MAL instruction using true MIPS machine instructions. In order to minimize register usage, you must implement this instruction by using only the 2 registers (Rs, Rt) specified in the instruction.

Hint: Observe that:

```A xor (A xor B) = B
B xor (A xor B) = A
```

## Question 2

Below is a part of the memory dump of the text section of a running MIPS program. The values on the left column inside [] are memory addresses. The values on the right column are contents of the memory. All the values are in hexadecimal numbers.

```   Address        Value
[0x0040006c]    0x3c081003
[0x00400070]    0x35089abc
[0x00400074]    0x8d040000
[0x00400078]    0x0c100023
[0x0040007c]    0x21080004
[0x00400080]    0x1040fffc
[0x00400084]    0x00000000
[0x00400088]    0x08100029
[0x0040008c]    0x00001020
[0x00400090]    0x14900002
[0x00400094]    0x20020001
[0x00400098]    0x08100028
[0x0040009c]    0x00048020
[0x004000a0]    0x03e00008
[0x004000a4]    0x1000ffff
```

Disassemble the code from the memory dump into MIPS instructions. You will need to make up unique labels for your assembly code. Comment your assembly code.

Also, briefly explain the function of this code.

## Question 3

The following table shows a program that is split into two MIPS assembly files, main.s and fact.s:

main.s fact.s
```      .global main

.data
num:  .word 4,1,3,0

.text
main:
sw    \$ra, 0(\$sp)
sw    \$s0, 4(\$sp)

la    \$s0, num
loop: lw    \$t0, 0(\$s0)
beq   \$t0, \$0, done
move  \$a0, \$t0
jal   fact
j     loop

done: lw    \$ra, 0(\$sp)
lw    \$s0, 4(\$sp)
jr    \$ra
```
```      .global fact
.text

fact:
li   \$v0, 1
beq  \$a0, \$0, done
mul  \$v0, \$a0, \$v0
beq  \$0, \$0, fact
done: li   \$t0, 0xcafe61c
slt  \$t0, \$v0, \$t0
bnez \$t0, exit
move \$v0, \$0
exit: jr   \$ra
```

### Part (A)

Pretend you are an assembler. Provide human-readable "object dump" of object file equivalents to assembling main.s and fact.s. You can use any format you like, as long as it contains enough information for the linker to relocate the labels. We suggest you use symbol/relocation tables similar to those in lecture and lab. You do not have to follow the exact output format of mips-objdump. However, you must clearly define your object dump file and table definitions. Show disassemble of the .text section of your object files as well.

Note that some of the instructions in the assembly files are pseudo instructions that you must take special care of. For example, the mul \$Rdest, \$Rsrc1, \$Rsrc2 instruction stores the lower 32-bit result of multiplying the values of register \$Rsrc1 and \$Rsrc2 into \$Rdest. There is no such instruction in MIPS machine language. Therefore you will need to translate that into the MIPS machine instruction mult and use mfhi or mflo to obtain result of mult.

### Part (B)

Pretend you are a linker. Show the final text and data segments, with the label main at address 0x00400000 and the data segment starting at 0x8fff0000. Show the symbol and relocation tables with all the values filled in.

Save your answer to both Part (A) and (B) of Question 3 to a file named hw6q3.txt.

# Submission

From the directory that contains files hw6q1.txt, hw6q2.s, hw6q3.txt, run:

```submit hw6
```