MIPS Function Continued

14
MIPS Function Continued

description

MIPS Function Continued. Function calls inside a function. What if we need to call another function inside a function? Will this work?. twofun: addi $sp, $sp, -4 sw $s0, 0($sp) jal addfun srl $s0, $a0, 1 sub $v0, $v0, $s0 lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra. - PowerPoint PPT Presentation

Transcript of MIPS Function Continued

Page 1: MIPS Function Continued

MIPS Function Continued

Page 2: MIPS Function Continued

Function calls inside a function

• What if we need to call another function inside a function? Will this work?

twofun:addi $sp, $sp, -4sw $s0, 0($sp)

jal addfunsrl $s0, $a0, 1sub $v0, $v0, $s0

lw $s0, 0($sp)addi $sp, $sp, 4

jr $ra

Page 3: MIPS Function Continued

Function calls inside a function

• The problem is that the value of $ra is changed whenever you use jal somelabel.

• How to deal with it?

twofun:addi $sp, $sp, -4sw $s0, 0($sp)

jal addfunsrl $s0, $a0, 1sub $v0, $v0, $s0

lw $s0, 0($sp)addi $sp, $sp, 4

jr $ra

Page 4: MIPS Function Continued

The working versions

twofun1:addi $sp, $sp, -4sw $s0, 0($sp)addi $sp, $sp, -4sw $ra, 0($sp)

jal addfunsrl $s0, $a0, 1sub $v0, $v0, $s0,

lw $ra, 0($sp)addi $sp, $sp, 4lw $s0, 0($sp)addi $sp, $sp, 4

jr $ra

twofun2:addi $sp, $sp,

-8sw $s0, 4($sp)sw $ra, 0($sp)

jal addfunsrl $s0, $a0, 1sub $v0, $v0,

$s0,

lw $ra, 0($sp)lw $s0, 4($sp)addi $sp, $sp,

8

jr $ra

Page 5: MIPS Function Continued

Saving registers

• In case of nested function calls, before calling a function, to be safe, the caller should– save $t0-$t9– save $a0-$a3If such registers are needed later. and – save $raBecause $ra is going to be needed later and it will be

changed• The callee should – save $s0-s7

Page 6: MIPS Function Continued
Page 7: MIPS Function Continued

Questions

Which of the following statements about MIPS stack is true?

(a) If a function has to modify $sp, it can save $sp on the stack by a “sw $sp, 0($sp)” instruction and restore it later by a “lw $sp, 0($sp)” instruction.(b) A “push” operation adds a new number to the stack. “push $t0” can be implemented as a pseudo instruction as “sw $t0, -4($sp).”(c) Both of the above.(d) None of the above.

Page 8: MIPS Function Continued
Page 9: MIPS Function Continued

04/19/2023 week04-3.ppt 9

Character and String Operations• Characters are encoded as 0’s and 1’s using ASCII

most commonly– American Standard Code for Information Interchange– Each character is represented using 8 bits (or a byte)

• MIPS provides instructions to move bytes– Load byte (lb) loads a byte to the rightmost 8 bits of a

register– Store byte (sb) write the rightmost 8 bits of a register to

memory

Page 10: MIPS Function Continued

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 11: MIPS Function Continued

04/19/2023 week04-3.ppt 11

String Copy Procedure

Page 12: MIPS Function Continued

.datamsg_hello: .asciiz "Hello\n“msg_empty: .space 400

.text

.globl mainMain: li $v0,4

la $a0,msg_hellosyscall

li $v0,4la $a0,msg_emptysyscall

la $a0,msg_empty #dstla $a1,msg_hello #srcjal strcpy

li $v0,4la $a0,msg_emptysyscall

li $v0,10 #exitsyscall

strcpy: lb $t0, 0($a1)sb $t0, 0($a0)addi $a0, $a0, 1addi $a1, $a1, 1bne $t0, $0, strcpyjr $ra

Page 13: MIPS Function Continued

Stack• Key things to keep in mind:

– Stack is a software concept – last in first out, that’s it.– In MIPS, you implement the stack by yourself by keeping $sp

always pointing to the top element on the stack– Stack can be used in functions to save register values, and is the

standard approach to save register values. But• You can also use stack for other purposes• This is not the only way to save register values.

Page 14: MIPS Function Continued

.datamsg: .asciiz "hello world"endl: .asciiz "\n"

.text .globl mainmain: addi $sp,$sp,-1

sb $0,0($sp)la $t1, msg

L0: lb $t0,0($t1)beq $t0,$0, L1addi $sp,$sp,-1sb $t0,0($sp)addi $t1,$t1,1j L0

L1: la $t1,msgL2: lb $t0,0($sp)

addi $sp,$sp,1sb $t0,0($t1)beq $t0, $0, L3addi $t1,$t1,1j L2

L3: la $a0,msgli $v0,4syscall

la $a0,endlli $v0,4syscall

li $v0,10 #exitsyscall