MIPS Function Continued Function calls inside a function

  • Slides: 14
Download presentation
MIPS Function Continued

MIPS Function Continued

Function calls inside a function • What if we need to call another function

Function calls inside a function • What if we need to call another function inside a function? Will this work? twofun: addi $sp, -4 sw $s 0, 0($sp) jal addfun srl $s 0, $a 0, 1 sub $v 0, $s 0 lw $s 0, 0($sp) addi $sp, 4 jr $ra

Function calls inside a function • The problem is that the value of $ra

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, -4 sw $s 0, 0($sp) jal addfun srl $s 0, $a 0, 1 sub $v 0, $s 0 lw $s 0, 0($sp) addi $sp, 4 jr $ra

The working versions twofun 1: addi $sp, -4 sw $s 0, 0($sp) addi $sp,

The working versions twofun 1: addi $sp, -4 sw $s 0, 0($sp) addi $sp, -4 sw $ra, 0($sp) twofun 2: addi $sp, -8 sw $s 0, 4($sp) sw $ra, 0($sp) jal addfun srl $s 0, $a 0, 1 sub $v 0, $s 0, lw $ra, 0($sp) addi $sp, 4 lw $s 0, 0($sp) addi $sp, 4 jal addfun srl $s 0, $a 0, 1 sub $v 0, $s 0, lw $ra, 0($sp) lw $s 0, 4($sp) addi $sp, jr $ra 8 jr $ra

Saving registers • In case of nested function calls, before calling a function, to

Saving registers • In case of nested function calls, before calling a function, to be safe, the caller should – save $t 0 -$t 9 – save $a 0 -$a 3 If such registers are needed later. and – save $ra Because $ra is going to be needed later and it will be changed • The callee should – save $s 0 -s 7

Questions Which of the following statements about MIPS stack is true? (a) If a

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 $t 0” can be implemented as a pseudo instruction as “sw $t 0, -4($sp). ” (c) Both of the above. (d) None of the above.

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

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 3/5/2021 week 04 -3. ppt 9

SPIM syscalls li $v 0, 1 li $a 0, 100 syscall # print an

SPIM syscalls li $v 0, 1 li $a 0, 100 syscall # print an integer in $a 0 li $v 0, 5 syscall # read an integer into $v 0 li $v 0, 4 # print an ASCIIZ string at $a 0 la $a 0, msg_hello syscall li $v 0, 10 syscall #exit

String Copy Procedure 3/5/2021 week 04 -3. ppt 11

String Copy Procedure 3/5/2021 week 04 -3. ppt 11

. data msg_hello: . asciiz "Hellon“ msg_empty: . space 400 Main: . text. globl

. data msg_hello: . asciiz "Hellon“ msg_empty: . space 400 Main: . text. globl main li $v 0, 4 la $a 0, msg_hello syscall li $v 0, 4 la $a 0, msg_empty syscall la $a 0, msg_empty #dst la $a 1, msg_hello #src jal strcpy li $v 0, 4 la $a 0, msg_empty syscall li $v 0, 10 #exit syscall strcpy: lb $t 0, 0($a 1) sb $t 0, 0($a 0) addi $a 0, 1 addi $a 1, 1 bne $t 0, $0, strcpy jr $ra

Stack • Key things to keep in mind: – Stack is a software concept

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.

. data. asciiz "hello msg: world" endl: . asciiz "n" main: L 0: .

. data. asciiz "hello msg: world" endl: . asciiz "n" main: L 0: . text. globl main addi $sp, -1 sb $0, 0($sp) la $t 1, msg lb $t 0, 0($t 1) beq $t 0, $0, L 1 addi $sp, -1 sb $t 0, 0($sp) addi $t 1, 1 j L 0 L 1: L 2: la $t 1, msg lb $t 0, 0($sp) addi $sp, 1 sb $t 0, 0($t 1) beq $t 0, $0, L 3 addi $t 1, 1 j L 2 L 3: la $a 0, msg li $v 0, 4 syscall la $a 0, endl li $v 0, 4 syscall li $v 0, 10 #exit syscall