CSE 2021: Computer Organization Lecture-6 Code Translation-4 Review & Exercises Shakil M. Khan (adapted from Prof. Roumani)
MIPS Assembly Language (Summary) 32X32 bits Registers ($s0 - $s7, $t0 - $t9, $zero, ) Arithmetic and logical instructions (add, sub, mult, div, slt, sll, srl, sra, the i/u/v suffix) Jump and branch (j, beq, bne) Load and store (lb, lh, lw, sb, sw, sh, the u suffix) I/O (syscall) Static attributes and.data (directives:.byte,.half,.word,.ascii,.asciiz,.space),.globl,.text Procedures (jal, jr, $v0 - $v1, $a0 - $a3, $ra) Stack ($sp, $fp) Heap (syscall 9) lui, mfhi, mflo, Some pseudo-instructions (la, mul, ) CSE-2021 June-14-2012 2
MIPS Assembly Language (Summary) MIPS instruction formats (R-type, I-type, J-type) MIPS addressing modes (immediate, register, base, PCrelative, pseudo-direct) Next: Concurrent data access (ll, sc) Translating (compiler + assembly) and object files Linking (static vs. dynamic) and loading Translating and starting up in Java (JVM, JIT compiler) CSE-2021 June-14-2012 3
Pseudo Instructions Not real, merely conveniences added by the assembler Not portable: have no machine language encoding Examples: mul mult + mfhi nop sll $0, $0, 0 li/la ori + lui abs slt + beq + sub + $at move add + $zero blt slt + bne + $at Do NOT use pseudo in this course! CSE-2021 June-14-2012 4
Exercise 1 Method pofu (product overflow unsigned) detects O/F in the mult of unsigned integers. It takes two integers in $a0 and $a1 and returns $v0 = 0 or 1 if $a0*$a1 fits or does not fit in 32 bits. Is it correct? If not, fix it. pofu: multu $a0, $a1 mfhi $t0 beq $t0, $0, fits addi $v0, $0, 1 fits: add $v0, $0, $0 jr $ra CSE-2021 June-14-2012 5
Exercise 2 Method pof is similar to pofu except it deals with signed integers. One quick way to implement pof would be to copy and paste pofu and then replace the label pofu with pof and the instruction multu with mult. Would this be correct? If not, fix it. CSE-2021 June-14-2012 6
Exercise 3(a) lib.s (below) was loaded with.text at 0x0040002C and.data at 0x10010000. What is the address of x?.globl x.globl ch.data x:.byte 3 y:.word 4.text ch: lw $v0, y($0) jr $ra CSE-2021 June-14-2012 7
Exercise 3(b) lib.s (below) was loaded with.text at 0x0040002C and.data at 0x10010000. What is the address of y? Why?.globl x.globl ch.data x:.byte 3 y:.word 4.text ch: lw $v0, y($0) jr $ra CSE-2021 June-14-2012 8
Exercise 3(c) lib.s (below) was loaded with.text at 0x0040002C and.data at 0x10010000. What is the address of ch?.globl x.globl ch.data x:.byte 3 y:.word 4.text ch: lw $v0, y($0) jr $ra CSE-2021 June-14-2012 9
Exercise 4(a) The file app.s (below) was loaded immediately after lib.s. What is the address of main?.text main: sw $ra, 0($sp) addi $sp, $sp, -4 jal ch lb $t0, x($0) hint: the assembler replaces lw/sw with several instructions CSE-2021 June-14-2012 10
Exercise 4(b) The file app.s (below) was loaded immediately after lib.s. What is the machine language encoding of the instruction: jal ch?.text main: sw $ra, 0($sp) addi $sp, $sp, -4 jal ch lb $t0, x($0) CSE-2021 June-14-2012 11
Exercise 5 Argue that stack allocations are not likely to cause memory leaks but that heap allocations are Factors: scope in which push/pop appear versus allocate/de-allocate consequences of imbalanced push/pop versus a missing de-allocate CSE-2021 June-14-2012 12
Exercise 6 Assume that the word 0x01048025 represents an instruction. What is the corresponding assembly language instruction? Your answer must use register names, e.g. $t0, not register numbers, and must specify immediate s in decimal, not binary. CSE-2021 June-14-2012 13
Exercise 7(a) To handle a large 32-bit immediate X, we replace lw $t0, X($a0) with the four instructions below: lui $at, upper16_x ori $at, $at, lower16_x add $at, $at, $a0 lw $t0, 0($at) Why can t the second be addi instead of ori? CSE-2021 June-14-2012 14
Exercise 7(b) To handle a large 32-bit immediate X, we replace lw $t0, X($a0) with the four instructions below: lui $at, upper16_x ori $at, $at, lower16_x add $at, $at, $a0 lw $t0, 0($at) Can you do it with just three instructions? CSE-2021 June-14-2012 15
Exercise 8 Given the utility class:.globl work.text work: slt $t0, $a0, $a1 bne $t0, $0, less add $v0, $a0, $a1 j done less: sub $v0, $a0, $a1 done: jr $ra What are the outputs of the following apps: CSE-2021 June-14-2012 16
Exercise 8(a).text main: sw $ra, 0($sp) addi $sp, $sp, -4 addi $a0, $0, 3 addi $a1, $0, 8 jal work add $a0, $v0, $0 addi $v0, $0, 1 syscall addi $sp, $sp, 4 lw $ra, 0($sp) jr $ra CSE-2021 June-14-2012 17
Exercise 8(b).text main: sw $ra, 0($sp) addi $sp, $sp, -4 addi $a0, $0, 36 addi $a1, $0, 8 sb $a0, work($a1) jal work add $a0, $v0, $0 addi $v0, $0, 1 syscall addi $sp, $sp, 4 lw $ra, 0($sp) jr $ra CSE-2021 June-14-2012 18
Exercise 9(a) What is the value of $s0 after executing label A? Write down the value of $s1 after executing label B.data x:.word 10 y:.byte 4, 3, 2 p:.word y.text main: addi $t0, $0, -4 A: lw $s0, p($t0) addi $t0, $0, 3 B: lb $s1, y($t0) jr $ra CSE-2021 June-14-2012 19
Exercise 9(b) What is the value of $s2 after executing label D? What is the value of $s3 after executing label E?.data x:.word 10 y:.byte 4, 3, 2 p:.word y.text main: addi $t0, $0, 1 addi $s2, $0, -3 C: sb $s2, y($t0) D: lb $s2, y($t0) E: lbu $s3, y($t0) jr $ra CSE-2021 June-14-2012 20
Exercise 10(a) Write the method clone that copies a given array of bytes to a new array. Specifically, it receives in $a0 the number of elements in the array and in $a1 the address of its first element. The method should allocate enough space on the heap to store the copy and then copy the array and return in $v0 the address of the first element of the copy. CSE-2021 June-14-2012 21
Exercise 10(b).data array1:.byte 1,2,11,4,5 array2:.space 5 Write a main method that: uses clone to copy array1 to the heap copies the content of the heap to array2 (cf. posted programs on the web) CSE-2021 June-14-2012 22