+ All Categories
Home > Documents > In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5...

In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5...

Date post: 03-Jan-2016
Category:
Upload: tyler-ryan
View: 220 times
Download: 4 times
Share this document with a friend
Popular Tags:
21
In Class Execise
Transcript
Page 1: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

In Class Execise

Page 2: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

.data

A: .word 0,1,2,3,4,5,6,7,8,9

.text

.globl main

main:

la $a0,A

li $a1,6

li $a2,5

jal onUpStream

done:

li $v0, 10 # exit

syscall

onUpStream:

onUpStream_LOOP:

lw $t0, 0($a0)

beq $t0, $a1, onUpStream_NO

beq $t0, $a2, onUpStream_YES

addi $a0, $a0, 4

j onUpStream_LOOP

onUpStream_YES:

ori $v0, $0, 1

j onUpStream_DONE

onUpStream_NO:

ori $v0, $0, 0

onUpStream_DONE:

jr $ra

Page 3: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

SPIM Syscalls

Page 4: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

SPIM syscalls

li $v0,1 # print an integer in $a0li $a0,100syscall

li $v0,5 # read an integer into $v0syscall

li $v0,4 # print an ASCIIZ string at $a0la $a0,msg_hellosyscall

li $v0,10 #exitsyscall

Page 5: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Pseudo instructions

Page 6: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Pseudo instructions

• MIPS supports pseudo instructions. We have seen some like – li $t0, 4

which set $t0 to 4. – la $t0, A

which puts the address of label A (a 32-bit value) into $t0.

– bgt $t0, $t1, L1

which goes to L1 if $t0 > $t1

Page 7: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Pseudo instructions

• Pseudo instructions are not real instructions implemented in hardware. They are created to make the program more readable.

• A pseudo instruction usually (not always) maps to several real instructions. The mapping is one-to-one.

Page 8: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Pseudo instructions

• For example, li $t0, 4

translate toori $t0, $0, 4

but what shouldli $t0, 90000

translate to?

Page 9: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Pseudo instructions

• Soli $t0, 90000

translates tolui $1, 1 #load upper 16 bits

ori $t0, $1, 24464

• The special register $1 is $at and should only be used for pseudo instructions.

Page 10: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Pseudo instructions

• How to translate ``lw $t0, val’’ ?

Page 11: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

MIPS mul div, and MIPS floating point instructions

Page 12: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Multiply and Division Instructions

• mul rd, rs, rt – put the result of rs times rt in rd

• div rd, rs, rt – A pseudo instruction– put the quotient of rs/rt into rd

Page 13: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

hi and lo

• mult rs,rt– put the high word in hi and low word in lo.

• div rs, rt – put the remainder in hi and quotient in lo.

Page 14: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Load and Store

• Load or store from a memory location (pseudoinstruction ). Just load the 32 bits into the register. – l.s $f0, val– s.s $f0, val

• Load immediate number (pseudoinstruction )– li.s $f0, 0.5

Page 15: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Print and Read

• Print: – li $v0, 2– li $f12, 0.5– syscall

• Read – li $v0, 6– syscall– (the read will be in $f0)

Page 16: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Arithmetic Instructions

• abs.s $f0, $f1• add.s $f0, $f1, $f2 • sub.s $f0, $f1, $f2 • mul.s $f0, $f1, $f2 • div.s $f0, $f1, $f2 • neg.s $f0, $f1

Page 17: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Data move

• mov.s $f0, $f1

copy $f1 to $f0. • mfc1 $t0, $f0

copy $f1 to $t0. • mtc1 $t0, $f0

copy $t0 to $f0.

Page 18: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Convert to integer and from integer

• cvt.s.w $f0, $f1 – convert the 32 bit in $f1 currently representing

an integer to float of the same value and store in $f0

• cvt.w.s $f0, $f1 – the reverse

Page 19: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Comparison instructions• c.lt.s $f0,$f1 – set a flag in coprocessor 1if $f0 < $f1, else clear it. The flag

will stay until set or cleared next time• c.le.s $f0,$f1 – set flag if $f0 <= $f1, else clear it

• bc1t L1 – branch to L1 if the flag is set

• bc1f L1 – branch to L1 if the flag is 0

• Where does the bc1t instruction take place? The main processor or the coprocessor?

Page 20: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

Computing the square root of a number n

• The Newton’s methodx’=(x+n/x)/2

– For any n, guess an initial value of x as the sqrt of n and keep on updating x until is the difference between the two updates are very close.

– The idea is that x’=x-f(x)/f’(x), where f(x) is x2-n=0.

Page 21: In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:

.dataval1: .float 0.6val2: .float 0.8

msg_done: .asciiz "done\n"

.text

.globl mainmain:

li.s $f0, 361.0mfc1 $a0, $f0jal calsqrt

done:mtc1 $v0, $f12li $v0,2syscall

eixt:li $v0,10syscall

# calsqrt: # calculating the square root of n# using the formular x'=(x+n/x)/2# loop until |x'-x| < 0.001

calsqrt:addi $sp, $sp, -24swc1 $f0, 20($sp)swc1 $f1, 16($sp)swc1 $f2, 12($sp)swc1 $f3, 8($sp)swc1 $f20, 4($sp)swc1 $f21, 0($sp)

mtc1 $a0, $f0 # $f0 gets nli.s $f20, 2.0 # $f20 storing constant 2 for dividingli.s $f21, 0.001 # $f21 storing constant 0.001 for exit

comparisiondiv.s $f1, $f0, $f20 # $f1 gets n/2

calsqrtloop:div.s $f2, $f0, $f1 # $f2 gets n/xadd.s $f2, $f2, $f1 # $f2 gets n/x + xdiv.s $f2, $f2, $f20 # $f2 gets x'=(n/x + x)/2sub.s $f3, $f2, $f1 # $f3 gets x'-xabs.s $f3, $f3 # $f3 gets |x'-x|c.lt.s $f3, $f21 # set the flag if |x'-x| < 0.001bc1t calsqrtdone mov.s $f1, $f2j calsqrtloop

calsqrtdone:mfc1 $v0, $f2

lwc1 $f0, 20($sp)lwc1 $f1, 16($sp)lwc1 $f2, 12($sp)lwc1 $f3, 8($sp)lwc1 $f20, 4($sp)lwc1 $f21, 0($sp)addi $sp, $sp, 24

jr $ra


Recommended