SPIM Versions. Outline SPIM Simulator. MIPS Assembly Language Programming. SPIM: MIPS Simulation. SPIM: MIPS Assembly Program 3/18/ PDF

Please download to get full document.

View again

of 10
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report
Category:

Poems

Published:

Views: 5 | Pages: 10

Extension: PDF | Download: 0

Share
Related documents
Description
MIPS: Assembly Language Programming A. Sahu CSE, IIT Guwahati Outline SPIM Simulator Demo program, System call MIPS Assembly Language Programming Overall Program: Comparison with C Stack, Call/Return Recursive
Transcript
MIPS: Assembly Language Programming A. Sahu CSE, IIT Guwahati Outline SPIM Simulator Demo program, System call MIPS Assembly Language Programming Overall Program: Comparison with C Stack, Call/Return Recursive Call : Factorial Example Please be updated with SPIM: MIPS Simulation MIPS Assembly Language Program Simulator: Course Website 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. SPIM Versions SPIM is the command line version. XSPIM is X Windows version (Unix workstations). There is also a Windows version. Ref: spimsimulator.sourceforge.net Ref: SPIM: MIPS Assembly 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. 1 Stack automatic (default), local Initialized/uninitialized Data Global, static, extern BSS: Block Started by Symbol BBS: Uninitialized Data Seg. Code program instructions Heap malloc, calloc Stack Heap BSS Data Code int A; int B=10; main(){ int Alocal; int *p; p=(int*)malloc(10); Block Started with Symbols: Un initialized Stack Heap BSS Data Code Segmentation fault: Accessing unallocated area int A[6]; x= A[8]; A[9]=k; A[i*i+3]=y; Another example: char *p; //p=(char *)malloc(12*sizeof(char)); strcpy(p, Helloworld );/*Seg.Fault*/ free(p); strcpy(p, AgainWorld );/*Seg.Fault*/ How to remove segmentation fault $gcc test.c $./aout segmentation fault $gcc g test.c $gdb./aout ( gdb) run.. Program received signal SIGSEGV, Segmentation fault. 0x in main () at test.c:21 21 B[200][100]=5; (gdb) backtrace #0 0x in main () at test.c:21 General Layout Data definitions start with directive Code definition starts with directive text is the traditional name for the memory that holds a program. Usually have a bunch of subroutine definitions and a main. Int GA=500; int fung(){ int FA=200; FA=FA+GA; int main(){ int LA=100; LA=LA+GA; return 0; C Example $gcc S test.c 2 .file test.c .globl GA.align 4.type GA, 4 GA:.long 500.globl fung.type X86 Assembly code.lfb0: ;; Local Fun Begin 0.cfi_startproc pushl %ebp ;; Push current FP.cfi_def_cfa_offset 8.cfi_offset 5, 8 movl %esp, %ebp.cfi_def_cfa_register 5 subl $16, %esp movl $200, 4(%ebp) ;; Local FA movl GA, %eax addl %eax, 4(%ebp) leave.cfi_restore 5.cfi_def_cfa 4, 4 ret.cfi_endproc.lfe0: ret ;;Local Fun End 0.size fung,. fung.globl main.type main:.lfb1: ;; local Fun Begin 1.cfi_startproc pushl %ebp.cfi_def_cfa_offset 8.cfi_offset 5, 8 movl %esp, %ebp.cfi_def_cfa_register 5 subl $16, %esp movl $100, 4(%ebp) movl GA, %eax addl %eax, 4(%ebp) X86 Assembly code movl $0, %eax leave.cfi_restore 5.cfi_def_cfa 4, 4 ret.cfi_endproc. LFE1 L lf E d1.size main,. main.ident GCC: (GNU) (Red Hat cfi offset 5 8.LFE1: ;;Local Fun End 1 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 Data Examples # data memory prompt:.asciiz Enter a number: msg:.asciiz The answer is x:.word 25 #32 bit variable y:.word 50 str:.space 100 Simple Example # data memory x.word 0 # 32 bit variable # program memory.align 2 # word alignment.globl main # main is global main: Simple I/O SPIM provides some simple I/O using the instruction. The specific I/O done depends on some registers. You set $v0 to indicate the operation. Parameters in $a0, $a1 3 Register Names and Purpose Name Register number Usage $zero 0 the constant value 0 $v0-$v1 2-3 values for results $a0-$a3 4-7 arguments $t0-$t temporaries $s0-$s saved $t8-$t more temporaries $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address I/O Functions $v0 Function Parameter 1 print_int $a0 is int 4 print_string $a0 is address of string 5 read_int returned in $v0 8 read_string $a0 is address of buffer, $a1 is length SPIM: Hello World ALP msg:.asciiz Hello World You set $v0 to indicate the operation. Parameters in $a0, $a1.globl main main: li $v0, 4 # 4 (print_str) la $a0, msg # argument: string # print the string Hello World: Printing a string msg:.asciiz Hello World main: li $v0,4 la $a0,msg jr $ra # retrun to caller Example: Reading an int See the demo HelloWorld MIPS ALP addi $v0,$zero,5 # now $v0 has the integer typed by # now $v0 has the integer typed by # a human in the SPIM console 4 SPIM subroutines The stack is set up for you just use $SP You can view the stack in the data window. main is called as a subroutine (have it return using jr $ra). Another MIPS ALP Example Overall program A:.space 400 la $1A $s1, # lui i$1a $s1, upper ; ori $1$1A $s1, $s1, lower li $s2, 100 # ori $s2, $zero, 100 code for input code for computation code for output Writing a simple loop for Σ i A[i] L: s = s+a[i]; Writing a simple loop for Σ i A[i] Pseudo instruction blt L: s = s+a[i]; move $s0, $zero move $t0, $zero L: muli $t1, $t0, 4 add $t1, $t1, $s1 $1# # $s1=&a[0] lw $t2, 0($t1) add $s0, $s0, $t2 add $t0, $t0, 1 blt $t0, $s2, L # $s2 = n blt r1, r2, label is equivalent to slt $at, r1, r2 bne $at, $zero, label 5 Overall program Improving code: pointer vs. index A:.space 400 la $1A $s1, # lui i$1a $s1, upper ; ori $1$1A $s1, $s1, lower li $s2, 100 # ori $s2, $zero, 100 code for input code for computation code for output L: s = s+a[i]; L: s = s + *p; Improving code: pointer vs. index L: s = s + *p; move $s0, $zero move $t0, $zero la $t1, A L: lw $t2, 0($t1) add $s0, $s0, $t2 addi $t1, $t1, 4 add $t0, $t0, 1 blt $t0, $s2, L # $s2 = n Improving code further L: s = s + *p; q = p+100; L: s = s + *p; if (p q) goto L; q = p+100; L: s = s + *p; Improving code further if (p q) goto L; move $s0, $zero la $t1, A addi $s2, $t1, 400 L: lw $t2, 0($t1) add $s0, $s0, $t2 addi $t1, $t1, 4 blt $t1, $s2, L Assembly Lang. Program ( S= i ) main: Prompt:.asciiz Please enter a +integer: result1:.asciiz Sum # main program with var n: $s0, sum:$s1,i:$s2 li $v0, 4 # issue prompt la $a0, prompt li $v0, 5 # get n from user 6 Assembly Lang. Program (S= i) move $s0, $v0 for: blt $s0, $s2, endf # exit loop if n i add $s1, $s1, $s2 # add ito sum add $s2, $s2, 1 # increment i b for # continue loop endf: li $v0, 4 # print Sum is la $a0, result1 li $v0, 1 # print sum move $a0, $s1 MIPS ALP Advanced Concept :Procedure and Stack li $v0, 10 # terminate the program Procedural Abstraction What is required? P(){ Q(){ Main P(); Q(); proc P proc Q Control flow (call and return) Data flow (parameter passing) Local and global storage allocation Take care of nesting Take care of recursion Control flow call Control flow return q = p + 99; X: r = p + 1; Y: xchg( ); r++; if (r q) goto Y; if (p q) goto X; la $t1, A addi $s2, $t1, 396 X: addi $t2, $t1, 4 Y: jal xchg # $ra gets PC+4 addi $t2, $t2, 4 ble $t2, $s2, Y addi $t1, $t1, 4 blt $t1, $s2, X void xchg( ) { if (*p *r) goto Z; *p *r; Z: return; xchg: lw $t3, 0($t1) lw $t4, 0($t2) blt $t3, $t4, Z sw $t3, 0($t2) sw $t4, 0($t1) Z: jr $ra # PC gets $ra 7 Parameter passing thro registers q = p + 99; X: r = p + 1; Y: xchg(p, r); r++; if (r q) goto Y; if (p q) goto X; convention la $a0, A addi $s2, $a0, 396 X: addi $a1, $a0, 4 Y: jal xchg # $ra gets PC+4 addi $a1, $a1, 4 ble $a1, $s2, Y addi $a0, $a0, 4 blt $a0, $s2, X Passing many parameters Convention: Input to procedure $a0, $a1, $a2, $a3 Output from procedure $v0, $v1 Additional parameters: Use memory Local storage allocation Nested calls main P Q data code P( ); void P( ) { void Q( ) { Q( );... return return jal P P:.. Q:.. jal Q... jr $ra jr $ra save and restore $ra Nested call example Nested call example main q = p + 99; X: r = p + 1; min(p, r); if (p q) goto X; void min(int *p, *r){ Y: xchg(p, r); r++; if (r q) goto Y; void xchg(int *p,*r ) { if (*p *r) goto Z; *p *r; Z: return; q = p + 99; X: r = p + 1; min(p, r); if (p q) goto X; la $a0, A addi $s2, $a0, 396 X: addi $a1, $a0, 4 jal min addi $a0, $a0, 4 blt $a0, $s2, X 8 Nested call example proc min Recursive calls control flow void min(int *p,*r){ Y: xchg(p, r); r++; if (r q) goto Y; min: sw $ra, ra_save Y: jal xchg addi $a1, $a1, 4 ble $a1, $s2, Y lw $ra, ra_save jr $ra void min(int *p,*r){ xchg(p, r); r++; if (r q) min(p, r); min: push $ra sw $ra, ra_save jal xchg addi $a1, $a1, 4 bgt $a1, $s2, R jal min pop $ra R: lw $ra, ra_save jr $ra Stack and push/pop operations MIPS ALP: Factorial Example $sp is the stack pointer push $ra addi $sp, $sp, 44 sw $ra, 0($sp) pop $ra lw $ra, 0($sp) addi $sp, $sp, 4 $sp int factorial (int n){ if (n 2) return 1; return (n factorial (n 1)); MIPS ALP: Factorial Example factorial: bgtz $a0, doit li $v0, 1 # base 0! = 1 jr $ra doit: sub $sp,8 # stack frame sw $s0,($sp) # for arg n sw $ra,4($sp) # ret addr move $s0, $a0 # save argument sub $a0, 1 # n 1 jal factorial # v0 = (n 1)! mul $v0,$s0,$v0 # n*(n 1)! # restore reg frm stack lw $s0,($sp) lw $ra,4($sp) add $sp,8 jr $ra Recursive calls data flow Stack is used for passing parameters when registers are not sufficient Stack is used for allocating local variables 9 Sharing registers Registers which are shared between caller and callee should be saved at the time of call and restored at the time of return Who does this? Conventions for saving registers $s0 to $s7 are called saved temporary these are preserved across calls saved by callee, if necessary $t0 to $t9 are called temporary these are not preserved across calls saved by caller, if necessary 10
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks