Homework 2 - Solutions (Assembly Language and Machine Language) Maximum Points: 40 Points Directions
Homework 2 - Solutions (Assembly Language and Machine Language) Maximum Points: 40 Points Directions
Homework 2 - Solutions (Assembly Language and Machine Language) Maximum Points: 40 Points Directions
Homework 2 - Solutions
(Assembly Language and Machine Language)
Maximum points : 40 points
Directions
This assignment is due Friday, Dec. 19th for Sections 1 and 2. Submit your solutions on a
separate sheet of paper.
Learning Objectives
In the process of completing this homework assignment, students will develop their
abilities to
• Translate assembly language instructions into machine language
• Interpret a sequence of bits and determine what it represents.
• Determine addressing in branches and jumps.
• Interpret instruction formats and fields.
• Implement algorithms procedural abstraction in assembly language.
Problems
1. [6 points] In Homework 1, you wrote a program that could calculate the product of two
numbers. Re-write the program, so that the main program reads the two values from
memory, uses procedure “Product” to determine the product of the two numbers, and
writes the product into a location in memory. You must also write the procedure
“Product”. Again, you may NOT use the MIPS mult, multu, mul, mulo or
muluo instructions. You MUST use a loop structure. The program must follow the
MIPS register conventions (page 138, Figure 3.11).
.text
.globl main
main:
la $s0, A
lw $s0, 0($s0) # Read the value of “A” from memory
la $t1, B
lw $s1, 0($t1) # Read the value of “B” from memory.
move $a0, $s0 # Copy the arguments "A" and "B" into the
# argument registers
move $a1, $s1
jal Product # Call procedure "Product"
move $t4, $v0 # Store return value "c" in temporary
# register.
la $t3, C
sw $t4, 0($t3) # Store the value of “c” in memory
li $v0, 10
syscall # Ready to quit
CSSE 232 – Computer Architecture I Rose-Hulman Institute of Technology
Winter 2003-2004 Computer Science and Software Engineering
Prof. Archana Chidanandan
loop:
beq $t3, $t1, done # while (i is not equal to b )
# continue adding a to c.
add $t4, $t4, $t0 # c= c + a;
addi $t3, $t3, 1 # i = i + 1
j loop
done:
move $v0, $t4 # Move return value "c" to the return
# value register.
jr $ra # Return to the calling procedure
.data
A: .word 5
B: .word 20
C: .word 0
main:
li $s0, 2 # Assign the x-pos
li $s1, 3 # Assign the x-vel
li $s2, 5 # Assign the y-pos
li $s3, 7 # Assign the y-vel
loop:
move $a0, $s0 # Move x-pos to $a0
move $a1, $s1 # Move x-vel to $a1
move $a2, $s2 # Move y-pos to $a2
move $a3, $s3 # Move y-vel to $a3
CSSE 232 – Computer Architecture I Rose-Hulman Institute of Technology
Winter 2003-2004 Computer Science and Software Engineering
Prof. Archana Chidanandan
done:
addi $v0, $zero, 10 # Ready to exit
syscall
MoveBall:
la $t0, RA # Load address of RA into $t0
sw $ra, 0($t0) # Store return address in "main" to
# memory
# location addressed as RA
move $t1, $a0 # Move x-pos and x-vel to registers $t1-2
move $t2, $a1
la $t0, STwo
sw $s2, 0($t0)
la $t0, STwo
lw $s2, 0($t0)
la $t0, SThree
lw $s3, 0($t0)
la $t0, RA
lw $ra, 0($t0) # Load from memory the return address in main
jr $ra # Go back to main
MoveObject:
add $t0, $a0, $0 # position component
add $t1, $a1, $0 # velocity component
cklb:
slt $t2, $t0, $0 # set flag if component is less than zero
beq $t2, $0, ckub # if flag is clear, ball didn't "cross"
# wall at zero
addi $t0, $t0, 11 # bring ball back in the other side
j cklb # a fast ball could "cross" more than
# once
ckub:
slti $t2, $t0, 11 # set flag if component is not at least
# eleven
bne $t2, $0, exit # if flag is set, ball didn't "cross"
# wall at eleven
addi $t0, $t0, -11 # bring ball back in the other side
j ckub # a fast ball could "cross" more than
# once
exit:
li $a2, -1
li $a3, -1
li $v1, -1
jr $ra
RA: .word 0
SOne: .word 0
STwo: .word 0
SThree: .word 0
3. [6 pts, 2 each] Bits have no inherent meaning. However, given the rules to interpret
them, a sequence of bits can represent different values and have meaning. Given the bit
pattern, 0x8dad0000, what does it represent, assuming that it is
a) an unsigned integer?
b) a 2’s complement number?
c) a MIPS instruction?
a) 0x8dad000 = (1000 1101 1010 1101 0000 0000 0000 0000)2 = (2376925184)10
d) MIPS instruction
4. [9 pts, 3 each] For the following MIPS assembly language instructions, list the
machine language fields and their binary and hexadecimal values:
a) ori $2, $0, 10
b) jal main # main is at address 0x00400020 and the current instruction
# is in the same page as “main”
c) add $t7, $t2, $0
CSSE 232 – Computer Architecture I Rose-Hulman Institute of Technology
Winter 2003-2004 Computer Science and Software Engineering
Prof. Archana Chidanandan
5. [4 pts] Assume that the MIPS instruction beq $t0, $s0, Label is located at
address 0x0400 5678, and that Label is located at address 0x0400 1234. What will the
binary value of the address field be? Hint: Remember that the offset is relative to the
instruction following the branch, and that all branch targets must be word aligned.
“j” instructions uses Pseudo-direct addressing. The 32-bit address needs to be fit into a
26-bit field.
a) Chop off the last two bits, as they are always “00” for any instruction address in MIPS.
b) Since the two instructions are on the same page (observe the most significant 4 bits,
they are equal), the most significant 4 bits need not encoded in the label field.
7. [3 pts] What sequence of MIPS instructions starting at address 0x0400 5678 could
be used to branch to address 0x4400 1234?
The two addresses are not on the same page(the 4 MSBs are not the same for the
addresses of the two instructions). Therefore, the “j” instruction cannot be used.
However, if the 32-bit value can be copied into a register, the “jr” instruction could be
used.
li $at, 0x44001234
jr $at