Computer Organization Lab #5 Refresh your memory Integer Registers Register Name Register Number Register Usage Use in my programs? $zero $0 Constant 0 Yes $at $1 Reserved for the assembler No $v0 - $v1 ** $2 - $3 Function result (low/high) Yes $a0 - $a3 ** $4 - $7 Argument Register 1 Yes $t0 - $t7 $8 - $15 Temporary registers Yes $s0 - $s7 $16 - $23 Saved registers Yes $t8 - $t9 $24 - $25 Temporary registers Yes $k0 - $k1 $26 - $27 Reserved for OS kernel No $gp $28 Global variables pointer No $sp ** $29 Stack pointer Yes $fp ** $30 Frame pointer Yes $ra ** $31 Return address (used by function call) Yes Procedures • We need procedures to: – Avoid repeating the same logic many times – Make our programs: • • • • • More modular Easier to read and understand Smaller in size More maintainable Less error prune Features of procedures 1. 2. 3. 4. 5. Name (Being) Arguments Local Data Body Return Value Image copied from a tutorial made by Joshua Cantrell The Procedure Being • For a procedure to have being, there must be a label to indicate its start address my_exist_procedure: # Address of first instruction of procedure. li $v0, 10 syscall • The label my_exit_procedure will be used to call the procedure Simplified Program Memory Structure FFFFFF08 FFFFFF04 FFFFFF00 Stack $sp=FFFFFF08 $sp=FFFFFF04 $sp=FFFFFF00 Heap ….. …… Data FFFF0008 FFFF0004 FFFF0000 Code $pc=FFFF0008 $pc=FFFF0004 $pc=FFFF0000 The stack • We use the stack: – To allocate local variables – To maintain the values of some registers not changed Registers Used for Procedures Register $pc Program Counter Points to the instruction in hand by the processor for execution $sp Stack Pointer Points to the head of the stack $fp Frame Pointer Points to the head of the stack when the function was called. Changes to the $sp doesn’t change the $fp $a0-$a3 Function arguments We use them to pass arguments to the functions $ra Return address We use it to keep the next instruction to which we will jump when the function ends $v0-$v1 Return value registers Store in them the values you want to return to the caller Calling a procedure FFFF0F9C Return to caller FFFF0F98 any logic FFFF0F94 any logic FFFF0F90 Increment_func: . . . FFFF0C9C any logic after calling FFFF0C98 Call Increment_func FFFF0C94 Prepare the argument a0 FFFF0C90 any logic $sp $fp $ra jal & jr instructions • Jal (jump and link): – Used to call a procedure – It makes 2 operations: • $ra = $pc+4 • Jump to function label • Jr (jump return: – It returns to the caller function – It makes 2 operations: • $pc = $ra • Jump to the return address in $ra Print Message Function • Write a function that takes a string as argument and then print it • We can agree that the argument $a0 to pass the string address Print Message Function .data msg: .asciiz "Hello World“ .globl main .text print_message: li $v0, 4 syscall jr $ra #function logic #function logic #return to caller main: la $a0, msg jal print_message li $v0, 10 syscall .end main #prepare the function parameters #call the function Your Task!!! • Write the hello world program and Debug it – Notice the change in the following registers: • PC • $ra • $sp Hurry!!!! Conventions for Using the registers in MIPS Functions 1. The static registers, $s0-$s7, must have the same value leaving a procedure as coming into a procedure. 2. The address in $ra register must be available at the end of the procedure to continue properly after the procedure has ended. 3. The addresses in $sp and $fp should be the same leaving as entering. 4. The registers, $t0-$t9, $a0-$a3, and $v0-v1 may be changed without restoring their previous values at the end. Using the stack to maintain the registers values procedure: addi $sp, $sp, -12 sw $ra, 8($sp) sw $s0, 4($sp) sw $s1, 0($sp) # allocate 3 words # save $ra # save $s0 # save $s1 addi $s0, $zero, 50 addi $s1, $zero, -23 add $a0, $s0, $s1 # $s0 = 50; # $s1 = -23; # $a0 = $s0 + $s1; jal procedure2 # procedure2($a0); lw $s1, 0($sp) lw $s0, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 jr $ra # # # # # restore $s1 restore $s0 restore $ra deallocate 3 words return; 88 84 $ra 80 $s0 76 $s1 72 Add 2 numbers function • Write a program that adds 2 integers • Use $a0 and $a1 to pass the 2 integers to the function • Use the stack to maintain the value for $ra Hurry!!!! .data num_1: .word 10 num_2: .word 20 .globl main .text add_two_numbers: addi $sp, $sp,-4 sw $ra, 0($sp) add $v0, $a0, $a1 lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra main: lw $a0, num_1 lw $a1, num_2 jal add_two_numbers add $a0, $v0, $0 li $v0, 1 syscall li $v0, 10 syscall .end main Announcements • Assignment 5 in published • Graded Practice Lab on 12/12 • Final Lab Exam on 19/12
© Copyright 2026 Paperzz