Docsity

SPIM Software-Assembly Language Programming-Lecture Slides, Slides of Assembly Language Programming

Partial preview of the text

Download SPIM Software-Assembly Language Programming-Lecture Slides and more Slides Assembly Language Programming in PDF only on Docsity!

SPIM

MIPS SIMULATION

  • SPIM is a simulator.
  • Reads a MIPS assembly language program.
  • Simulates each instruction.
  • Displays values of registers and memory.
  • Supports breakpoints and single stepping.
  • Provides simple I/O for interacting with user.

RESOURCES ON THE WEB

  • There’s a very good SPIM tutorial at

http://chortle.ccsu.edu/AssemblyTutorial/Chapter-09/ass09_1.html

  • In fact, there’s a tutorial for a good chunk of the ISA portion of this course at:

http://chortle.ccsu.edu/AssemblyTutorial/tutorialContents.html

  • Here are a couple of other good references you can look at:

Patterson_Hennessy_AppendixA.pdf

And http://babbage.clarku.edu/~jbreecher/comp_org/labs/Introduction_To_SPIM.pdf

SPIM PROGRAM

  • MIPS assembly language.
  • Must include a label “main” – this will be called by the SPIM startup code (allows you to have command line arguments).
  • Can include named memory locations, constants and string literals in a “data segment”.

SIMPLE EXAMPLE

.data # data memory foo: .word 0 # 32 bit variable

.text # program memory .align 2 # word alignment .globl main # main is global

main: lw $a0,foo

DATA DEFINITIONS

  • You can define variables/constants with:
  • .word : defines 32 bit quantities.
  • .byte: defines 8 bit quantities.
  • .asciiz: zero-delimited ascii strings.
  • .space: allocate some bytes.

MIPS: SOFTWARE CONVENTIONS FOR

REGISTERS

SIMPLE I/O

SPIM provides some simple I/O using the “syscall” instruction. The specific I/O done depends on some registers.

  • You set $v0 to indicate the operation.
  • Parameters in $a0, $a1.

EXAMPLE: READING AN INT

li $v0,5 # Indicate we want function 5 syscall

Upon return from the syscall, $v0 has the integer typed by

a human in the SPIM console

Now print that same integer

move $a0,$v0 # Get the number to be printed into register li $v0,1 # Indicate we’re doing a write -integer syscall

PRINTING A STRING

.data msg: .asciiz “SPIM IS FUN” .text .globl main: li $v0, la $a0,msg syscall jr $ra

WHY ARE STACKS SO GREAT?

  • Some machines provide a memory stack as part of the architecture (e.g., VAX)
  • Sometimes stacks are implemented via software convention (e.g., MIPS)

WHY ARE STACKS SO GREAT?

MIPS FUNCTION CALLING

CONVENTIONS

main() {

printf("The factorial of 10 is %d\n", fact(10));

int fact (int n) {

if (n <= 1) return(1);

return (n * fact (n-1));

MIPS FUNCTION CALLING

CONVENTIONS

.text .global main main: subu $sp, $sp, 32 #stack frame size is 32 bytes sw $ra,20($sp) #save return address li $a0,10 # load argument (10) in $a jal fact #call fact la $a0 LC #load string address in $a move $a1,$v0 #load fact result in $a jal printf # call printf lw $ra,20($sp) # restore $sp addu $sp, $sp,32 # pop the stack jr $ra # exit() .data LC: .asciiz "The factorial of 10 is %d\n"