MIPS mul div and MIPS floating point instructions

  • Slides: 13
Download presentation
MIPS mul div, and MIPS floating point instructions

MIPS mul div, and MIPS floating point instructions

SPIM • Run codes with SPIM. – Use any editor to write the source

SPIM • Run codes with SPIM. – Use any editor to write the source file. – Run PCSpim, load the source file. – F 10 to step through the code.

Complete MIPS code • It has a data segment and a code (text) segment.

Complete MIPS code • It has a data segment and a code (text) segment. • The beginning of the data segment in the assembly source code is indicated as . data and followed by several declarations such as – msg: . asciiz "hello world" meaning an asciiz string whose starting address is associated with label ``msg. ’’ – A: . word 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 meaning an array of words whose starting address is associated with label ``A. ’’ – msg_empty: . space 400 meaning 400 bytes in the memory whose starting address is associated with label ``msg_empty. ’’

Complete MIPS code • The text segment in the source code usually starts with

Complete MIPS code • The text segment in the source code usually starts with . text. globl main: where ``main’’ is the label associated with the address of the first instruction of the code. • And the code usually ends with li $v 0, 10 #exit syscall

Multiply and Division Instructions • mul rd, rs, rt – put the result of

Multiply and Division Instructions • mul rd, rs, rt – put the result of rs times rt in rd • div rd, rs, rt – put the quotient of rs/rt into rd

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

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.

Load and Store • Load or store from a memory location (pseudoinstruction ). Just

Load and Store • Load or store from a memory location (pseudoinstruction ). Just load the 32 bits into the register. – l. s $f 0, val – s. s $f 0, val • Load immediate number (pseudoinstruction ) – li. s $f 0, 0. 5

Print and Read • Print: – li $v 0, 2 – li $f 12,

Print and Read • Print: – li $v 0, 2 – li $f 12, 0. 5 – syscall • Read – li $v 0, 6 – syscall – (the read will be in $f 0)

Arithmetic Instructions • • • abs. s add. s sub. s mul. s div.

Arithmetic Instructions • • • abs. s add. s sub. s mul. s div. s neg. s $f 0, $f 0, $f 1, $f 1 $f 2

Data move • mov. s $f 0, $f 1 copy $f 1 to $f

Data move • mov. s $f 0, $f 1 copy $f 1 to $f 0. • mfc 1 $t 0, $f 0 copy $f 1 to $t 0. • mtc 1 $t 0, $f 0 copy $t 0 to $f 0.

Convert to integer and from integer • cvt. s. w $f 0, $f 1

Convert to integer and from integer • cvt. s. w $f 0, $f 1 – convert the 32 bit in $f 1 currently representing an integer to float of the same value and store in $f 0 • cvt. w. s $f 0, $f 1 – the reverse

Comparison instructions • c. lt. s $f 0, $f 1 – set a flag

Comparison instructions • c. lt. s $f 0, $f 1 – set a flag in coprocessor 1 if $f 0 < $f 1, else clear it. The flag will stay until set or cleared next time • c. le. s $f 0, $f 1 – set flag if $f 0 <= $f 1, else clear it • bc 1 t L 1 – branch to L 1 if the flag is set • bc 1 f L 1 – branch to L 1 if the flag is 0 • Where does the bc 1 t instruction take place? The main processor or the coprocessor?

Computing the square root of a number n • The Newton’s method x’=(x+n/x)/2 –

Computing the square root of a number n • The Newton’s method x’=(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 x 2 -n=0.