# Pseudo instructions Pseudo instructions MIPS supports pseudo instructions

• Slides: 17

Pseudo instructions

Pseudo instructions • MIPS supports pseudo instructions. We have seen some like – li \$t 0, 4 which set \$t 0 to 4. – la \$t 0, A which puts the address of label A (a 32 -bit value) into \$t 0. – bgt \$t 0, \$t 1, L 1 which goes to L 1 if \$t 0 > \$t 1

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.

Pseudo instructions • For example, li \$t 0, 4 translate to ori \$t 0, \$0, 4 but what should li \$t 0, 90000 translate to?

Pseudo instructions • So li \$t 0, 90000 translates to lui ori \$1, 1 #load upper 16 bits \$t 0, \$1, 24464 • The special register \$1 is \$at and should only be used for pseudo instructions.

Pseudo instructions • How to translate ``lw \$t 0, val’’ ?

MIPS mul div, and MIPS floating point instructions

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

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 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. s \$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. 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 0. • mfc 1 \$t 0, \$f 0 copy \$f 0 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 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 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

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.

val 1: val 2: . data. float 0. 6. float 0. 8 msg_done: . asciiz "donen". text. globl main: mtc 1 \$a 0, \$f 0 li. s \$f 20, 2. 0 li. s \$f 21, 0. 001 comparision div. s \$f 1, \$f 0, \$f 20 # \$f 1 gets n/2 calsqrtloop: div. s \$f 2, \$f 0, \$f 1 add. s \$f 2, \$f 1 div. s \$f 2, \$f 20 sub. s \$f 3, \$f 2, \$f 1 abs. s \$f 3, \$f 3 c. lt. s \$f 3, \$f 21 bc 1 t calsqrtdone mov. s \$f 1, \$f 2 j calsqrtloop li. s \$f 0, 361. 0 mfc 1 \$a 0, \$f 0 jal calsqrt done: mtc 1 \$v 0, \$f 12 li \$v 0, 2 syscall eixt: li \$v 0, 10 syscall # # calsqrtdone: calsqrt: calculating the square root of n using the formular x'=(x+n/x)/2 loop until |x'-x| < 0. 001 calsqrt: addi swc 1 swc 1 \$sp, -24 \$f 0, 20(\$sp) \$f 1, 16(\$sp) \$f 2, 12(\$sp) \$f 3, 8(\$sp) \$f 20, 4(\$sp) \$f 21, 0(\$sp) # \$f 0 gets n # \$f 20 storing constant 2 for dividing # \$f 21 storing constant 0. 001 for exit mfc 1 \$v 0, \$f 2 lwc 1 lwc 1 addi \$f 0, 20(\$sp) \$f 1, 16(\$sp) \$f 2, 12(\$sp) \$f 3, 8(\$sp) \$f 20, 4(\$sp) \$f 21, 0(\$sp) \$sp, 24 jr \$ra # # # \$f 2 \$f 3 set gets n/x + x gets x'=(n/x + x)/2 gets x'-x gets |x'-x| the flag if |x'-x| < 0. 001