hw5

.pdf

School

University Of Arizona *

*We aren’t endorsed by this school

Course

252

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

17

Uploaded by MagistrateQuetzal3896 on coursehero.com

CSc 252: Computer Organization HW 5 due at 5pm, two days before the test Turn in through GradeScope Policy Reminders You must turn in a single PDF to GradeScope. Other file formats will not be accepted. (Sorry, this is necessary for the sanity of the TAs!) You are allowed to work with other students on this homework, as we will not be grading it for correctness. However, each student must turn in their own copy of the homework. Show your work for all problems. While we won’t be grading for correctness, you will not receive full credit unless you show your work. After all, showing your work is required on the test - and homeworks are intended to help you practice for the test! Required Problems: 5.1(e-f), 5.2(d), 5.3(d), 5.4(f) Allowable Instructions When writing MIPS assembly, the only instructions that you are allowed to use (so far) are: add, addi, sub, addu, addiu, subu beq, bne, j, jal, jr slt, slti and, andi, or, ori, nor, nori, xor, xori sll, srl, sra lw, lh, lb, sw, sh, sb la syscall mult, div, mfhi, mflo While MIPS has many other useful instructions (and the assembler recognizes many pseudo-instructions), do not use them! We want you to learn the fundamentals of how assembly language works - you can use fancy tricks after this class is over.
Problem 5.1 - Dependencies In each part below, list all of the depdencies between instructions. I have numbered the instructions for convenience. 5.1(a) add $s2, $s5, $s4 # instruction 1 add $s4, $s2, $s5 # instruction 2 sw $s5, 100($s2) # instruction 3 add $s3, $s4, $s2 # instruction 4 5.1(b) add $s2, $s5, $s4 # instruction 1 add $s5, $s2, $s5 # instruction 2 sw $s5, 100($s2) # instruction 3 lw $s5, 104($s2) # instruction 4 NOTE: Think carefully about what is read, and what is written, in lw/sw. Where are the dependencies? 5.1(c) lw $t0, 0($s0) # instruction 1 sw $t0, 0($s1) # instruction 2 lw $t1, 0($t0) # instruction 3 beq $t0, $t1, LABEL # instruction 4 5.1(d) add $s2, $s0, $s1 # instruction 1 add $s3, $s1, $s2 # instruction 2 add $s4, $s2, $s3 # instruction 3 add $s5, $s3, $s4 # instruction 4 5.1(e) - Turn in this one la $t0, foo # instruction 1 lw $t1, 0($t0) # instruction 2 add $t2, $s0, $s1 # instruction 3 sub $t3, $t1, $t2 # instruction 4 5.1(f) - Turn in this one add $s2, $s0, $s1 # instruction 1 add $s5, $s3, $s4 # instruction 2 sub $s6, $s2, $s5 # instruction 3 addi $s7, $s6, 1 # instruction 4 Page 2
Problem 5.2 - Converting MIPS to C In the following problems, I will give you a snippet of MIPS assembly, which you will convert to C. Assume that all the MIPS code will use tX registers for temporary values (that is, values which are not given names in C), and that any sX registers represent variables which have names in C. If the code includes any .data section, then include exactly equivalent C declarations in your code. Also, if the code uses any sX registers, give C declarations for matching variables. The names don’t matter, but giving the proper types is important. The possible types are: int - MIPS words. Use this when you don’t know anything else. int* - Pointers to MIPS words or arrays of words. char* - Pointers to bytes or strings. To figure out types, you will have to use any number of clues - such as which variables are used in la, lw, sw instructions, how arrays are indexed, or what syscalls are used. Use comments to clearly show what register is associated with each variable name. Likewise, if the assembly calls a function, give a declaration (not a definition!) for the function, including what you can figure out about the parameters and return type (if any). Read the examples closely to see what I’m looking for. 5.2(a) .global myFunc myFunc: addiu $sp, $sp, -24 sw $fp, 0($sp) sw $ra, 4($sp) addiu $fp, $sp, 20 add $t0, $a0, $a1 add $t1, $a2, $a3 sub $v0, $t0, $t1 lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 24 jr $ra Page 3
5.2(b) .data str: .asciiz ...something... foo: .word 0 .text addi $s0, $zero, 0 la $s1, str LOOP: lb $t0, 0($s1) beq $t0, $zero, LOOP_END addi $t1, $zero, 0x78 # 0x78 = ’x’ bne $t0, $t1, FALSE addi $s0, $s0, 1 FALSE: addi $s1, $s1, 1 j LOOP LOOP_END: la $t0, foo sw $s0, 0($t0) Page 4
5.2(c) addi $a0, $zero, 1 jal doThatThing addi $t0, $v0, $zero addiu $sp, $sp, -4 sw $t0, 0($sp) addi $a0, $zero, 2 jal doThatThing addi $t1, $v0, $zero addiu $sp, $sp, -4 sw $t1, 0($sp) addi $a0, $zero, 3 jal doThatThing addi $t2, $v0, $zero lw $t1, 0($sp) lw $t0, 4($sp) addiu $sp, $sp, 8 addi $v0, $zero, 1 add $a0, $t0, $t1 add $a0, $a0, $t2 syscall addi $v0, $zero, 11 addi $a0, $zero, 0xa syscall Page 5
5.2(d) - Turn in this one .global problemPartD problemPartD: addiu $sp, $sp, -24 sw $fp, 0($sp) sw $ra, 4($sp) addiu $fp, $sp, 20 addiu $sp, $sp, -4 sw $s0, 0($sp) addi $s0, $zero, 0 LOOP: jal getAValue beq $v0, $zero, LOOP_END add $s0, $s0, $v0 j LOOP LOOP_END: add $v0, $s0, $zero lw $s0, 0($sp) addiu $sp, $sp, 4 lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 24 jr $ra Page 6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help