/
Organization Organization

Organization - PDF document

ximena
ximena . @ximena
Follow
342 views
Uploaded On 2021-06-18

Organization - PPT Presentation

CSE 2021 Computer Lecture 6 Code Translation 4 Review Exercises Shakil M Khan MIPS Assembly Language Summary 32X32 bits Registers s0 s7 t0 t9 zero Arithme ID: 844769

2021 cse exercise addi cse 2021 addi exercise text data word globl main add byte address jal instructions pofu

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "Organization" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1 CSE 2021: Computer Organization Le
CSE 2021: Computer Organization Lecture - 6 Code Translation - 4 Review & Exercises Shakil M. Khan 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)

2 • Jump and branch ( j , beq , bne )
• 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 ,

3 $ fp ) • Heap ( syscall 9) • l
$ fp ) • Heap ( syscall 9) • lui , mfhi , mflo , … • Some pseudo - instructions ( la , mul , …) CSE - 2021 2 MIPS Assembly Language (Summary) • MIPS instruction formats ( R - type , I - type , J - type ) • MIPS addressing modes ( immediate , register , base , PC - relative , pseudo - direct ) • Concurrent data

4 access ( ll , sc ) • Translating (
access ( ll , sc ) • Translating (compiler + assembly) and object files • Linking (static vs. dynamic) and loading CSE - 2021 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 â

5 €“ li / la ori + lui – abs s
€“ 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 4 Exercise 1 • Method pofu ( p roduct o ver f low u nsigned) detects O/F in the mult of unsigned integers. It takes two integers in $a0 and $a1 and

6 returns $v0 = 0 or 1 if $a0*$a1 fits or
returns $v0 = 0 or 1 if $a0*$a1 fits or does not fit in 32 bits. Is it correct? If not, fix it. CSE - 2021 5 pofu : multu $a0, $a1 mfhi $t0 beq $t0, $0, fits addi $v0, $0, 1 fits: add $v0, $0, $0 jr $ ra Exercise 2 • Method pof is similar to pofu except it deals wit

7 h signed integers. One quick way to imp
h 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 6 Exercise 3(a) • lib.s (below) was loaded with .text at 0x0040002C and .data at 0x10010000 . What is

8 the address of x ? CSE - 2021 7
the address of x ? CSE - 2021 7 . globl x . globl ch .data x: .byte 3 y: .word 4 .text ch : lw $v0, y($0) jr $ ra Exercise 3(b) • lib.s (below) was loaded with .text at 0x0040002C and .data at 0x10010000 . What is the address of y ? Why? CS

9 E - 2021 8 . globl x
E - 2021 8 . globl x . globl ch .data x: .byte 3 y: .word 4 .text ch : lw $v0, y($0) jr $ ra Exercise 3(c) • lib.s (below) was loaded with .text at 0x0040002C and .data at 0x10010000 . What is the address of ch ? CSE - 2021 9 . globl

10 x . globl ch
x . globl ch .data x: .byte 3 y: .word 4 .text ch : lw $v0, y($0) jr $ ra Exercise 4(a) • The file app.s (below) was loaded immediately after lib.s . What is the address of main ? • hint : the assembler replaces lw / sw with several instructions

11 CSE - 2021 10 .text main
CSE - 2021 10 .text main: sw $ ra , 0($sp) addi $sp, $sp, - 4 jal ch lb $t0, x($0) 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 ? CSE - 2021 11 .text main: sw

12 $ ra , 0($sp) addi
$ ra , 0($sp) addi $sp, $sp, - 4 jal ch lb $t0, x($0) Exercise 5 • Argue that stack allocations are not likely to cause memory leaks but that heap allocations are CSE - 2021 12 • Factors : – scope in which push/pop appear versus allocate/de - allocate – consequences of imbalanced

13 push/pop versus a missing de - allocat
push/pop versus a missing de - allocate 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 immediates in decimal, not binary. CSE - 2021 13 Exercise 7 • To ha

14 ndle a large 32 - bit immediate X , we
ndle a large 32 - bit immediate X , we replace with the four instructions below: • Why can’t the second be addi instead of ori ? • Can we do better (three instructions)? CSE - 2021 14 lw $t0, X($a0) lui $at, upper16_X ori $at, $at, lower16_X add $at, $at, $a0 lw $t0, 0($at) Exercise 8

15 • Given the utility class:
• Given the utility class: What are the outputs of the following apps: CSE - 2021 15 . 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 Exercise 8(a) CSE - 202

16 1 16 .text main: sw
1 16 .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 Exercise 8(

17 b) CSE - 2021 17 .text
b) CSE - 2021 17 .text main: sw $ ra , 0($sp) addi $sp, $sp, - 4 addi $a0, $0, 36 addi $a1, $0, 8 jal work add $a0, $v0, $0 addi $v0, $0, 1 syscall addi $sp, $sp, 4 lw $ ra , 0($sp) jr $

18 ra Exercise 9(a) • What is the va
ra Exercise 9(a) • What is the value of $s0 after executing label A ? • Write down the value of $s1 after executing label B CSE - 2021 18 .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)

19 jr $ ra Exercise 9(b) â€
jr $ ra Exercise 9(b) • What is the value of $s2 after executing label D ? • What is the value of $s3 after executing label E ? CSE - 2021 19 .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:

20 lb $s2, y($t0) E: lbu $s3, y(
lb $s2, y($t0) E: lbu $s3, y($t0) jr $ ra 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

21 the array and return in $v0 the addr
the array and return in $v0 the address of the first element of the copy. CSE - 2021 20 Exercise 10(b) • 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 21 .data array1: .byte 1,2,11,4,5