SlideShare ist ein Scribd-Unternehmen logo
1 von 30
MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker
MIPS Assembly Instructions ,[object Object],[object Object],[object Object],[object Object]
Assembler directives ,[object Object],[object Object],[object Object],[object Object]
Hello World! .text # code section .globl main main: li $v0, 4 # system call for print string la $a0, str # load address of string to print syscall # print the string li $v0, 10 # system call for exit syscall # exit .data str:  .asciiz “Hello world!”  # NUL terminated string, as in C
Addressing modes lw $s1, addr  # load $s1 from addr lw $s1, 8($s0) # $s1 = Mem[$s0+8] register  $s0  contains the base address access the address  ($s0) possibly add an offset  8($s0)
Load and move instructions la  $a0, addr # load address addr into $a0 li  $a0, 12 # load immediate $a0 = 12 lb $a0, c($s1) # load byte $a0 = Mem[$s1+c] lh $a0, c($s1)  # load half word lw $a0, c($s1) # load word move $s0, $s1 # $s0 = $s1
Control Structures ,[object Object],[object Object],[object Object],[object Object],[object Object]
Branch instructions beqz $s0, label if $s0==0 goto label bnez $s0, label if $s0!=0  goto label bge  $s0, $s1, label if $s0>=$s1  goto label ble  $s0, $s1, label  if $s0<=$s1  goto label blt  $s0, $s1, label  if $s0<$s1  goto label beq  $s0, $s1, label  if $s0==$s1  goto label bgez  $s0, $s1, label  if $s0>=0  goto label
if-then-else structures if ($t0==$t1) then /* blockA */ else /* blockB */   beq $t0, $t1, blockA j blockB blockA: 
 instructions of then block 
 j exit blockB: 
 instructions of else block 
 exit: 
 subsequent instructions 

repeat-until loop repeat 
 until $t0>$t1 loop:  
 instructions of loop 
 ble $t0, $t1, loop   # if $t0<=$t1 goto loop Other loop structures are similar
 Exercise:  Derive templates for various loop structures
System calls ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
SPIM system calls $a0 address of string 4 print string $f12 contains number 3 print double $f12 contains number 2 print float $a0 contains number 1 print int argument code $v0 procedure
SPIM system calls 8 read string res returned in $f0 7 read double res returned in $f0 6 read float res returned in $v0 5 read int result code $v0 procedure
Example programs ,[object Object],[object Object],1 2 3 for(i=0; i<len; i++) { a[i] = a[i] + 5; }
Print numbers 1 to 10 main: li $s0, 1 # $s0 = loop counter li $s1, 10 # $s1 = upper bound of loop loop: move $a0, $s0 # print loop counter $s0 li $v0, 1 syscall li $v0, 4 # print “” la $a0, linebrk # linebrk: .asciiz “” syscall addi $s0, $s0, 1 # increase counter by 1 ble $s0, $s1, loop # if ($s0<=$s1) goto loop li $v0, 10 # exit syscall
Increase array elements by 5 .text .globl main main:  la  $t0, Aaddr  # $t0 = pointer to array A lw  $t1, len  # $t1 = length (of array A) sll  $t1, $t1, 2  # $t1 = 4*length add  $t1, $t1, $t0  # $t1 = address(A)+4*length loop:  lw  $t2, 0($t0)  # $t2 = A[i] addi $t2, $t2, 5  # $t2 = $t2 + 5 sw  $t2, 0($t0)  # A[i] = $t2 addi $t0, $t0, 4  # i = i+1 bne  $t0, $t1, loop  # if $t0<$t1 goto loop .data Aaddr:  .word 0,2,1,4,5  # array with 5 elements len:  .word 5
Increase array elements by 5 .text .globl main main: la $t0, Aaddr # $t0 = pointer to array A lw $t1, len # $t1 = length (of array A) sll $t1, $t1, 2 # $t1 = 4*length (byte addr.) add $t1, $t1, $t0 # $t1 = beyond last elem. A
Increase array elements by 5 Loop: lw  $t2, ($t0) # $t2 = A[i] addi $t2, $t2, 5 # $t2 = $t2 + 5 sw  $t2, ($t0) # A[i] = $t2 addi $t0, $t0, 4 # i = i+1 bne  $t0, $t1, loop # if $t0<$t1 goto loop li $v0, 10 # exit syscall
Increase array elements by 5 .data Aaddr: .word 0,2,1,4,5 len: .word 5 Idiosyncratic:  Byte addressing => loop in steps of 4 Describe meaning of registers in your documentation!
Procedures ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Procedures ,[object Object],[object Object],[object Object],[object Object]
Write your own procedures # prints the integer contained in $a0 print_int:  li $v0, 1  # system call to  syscall  # print integer jr $ra  # return main: . . . li $a0, 10 # we want to print 10 jal print_int  # print integer in $a0
Write your own procedures .data linebrk: .asciiz “” .text print_eol:  # prints &quot;&quot; li $v0, 4  # la $a0, linebrk  # syscall  # jr $ra  # return main: . . . jal print_eol  # printf(“”)
Write your own procedures .data main: li $s0, 1  # $s0 = loop ctr li $s1, 10  # $s1 = upperbnd loop:  move $a0, $s0  # print loop ctr jal print_int  #  jal print_eol  # print &quot;&quot; addi $s0, $s0, 1  # loop ctr +1 ble $s0, $s1, loop  # unless $s0>$s1

Non-leaf procedures ,[object Object],[object Object],[object Object]
Stack high address low address stack pointer $sp --> ,[object Object],[object Object],[object Object],[object Object],[object Object],$sp = $sp - 12 0($sp) 4($sp) 8($sp)
Fibonacci fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) 0, 1, 1, 2, 3, 5, 8, 13, 21,

Fibonacci li $a0, 10  # call fib(10) jal fib #  move $s0, $v0  # $s0 = fib(10) fib is a recursive procedure with one argument $a0 need to store argument $a0, temporary register $s0 for intermediate results, and return address $ra
fib:  sub $sp,$sp,12  # save registers on stack sw $a0, 0($sp)  # save $a0 = n sw $s0, 4($sp)  # save $s0 sw $ra, 8($sp)  # save return address $ra bgt $a0,1, gen  # if n>1 then goto generic case move $v0,$a0  # output = input if n=0 or n=1 j rreg  # goto restore registers gen:  sub $a0,$a0,1  # param = n-1 jal fib  # compute fib(n-1) move $s0,$v0  # save fib(n-1) sub $a0,$a0,1  # set param to n-2 jal fib  # and make recursive call add $v0, $v0, $s0  # $v0 = fib(n-2)+fib(n-1) rreg:  lw  $a0, 0($sp)  # restore registers from stack lw  $s0, 4($sp)  # lw  $ra, 8($sp)  # add $sp, $sp, 12  # decrease the stack size jr $ra
Practice, practice, practice!!! ,[object Object],[object Object],[object Object],[object Object]

Weitere Àhnliche Inhalte

Was ist angesagt?

Signal descriptors of 8086
Signal descriptors of 8086Signal descriptors of 8086
Signal descriptors of 8086
aviban
 
PIN Specification of 8086 Microprocessor
PIN Specification of 8086 MicroprocessorPIN Specification of 8086 Microprocessor
PIN Specification of 8086 Microprocessor
Nikhil Kumar
 
Core I3 Vs Core I5
Core I3 Vs Core I5Core I3 Vs Core I5
Core I3 Vs Core I5
Ayeshasidhu
 
DDR2 SDRAM
DDR2 SDRAMDDR2 SDRAM
DDR2 SDRAM
Subash John
 

Was ist angesagt? (20)

Arm architecture
Arm architectureArm architecture
Arm architecture
 
Signal descriptors of 8086
Signal descriptors of 8086Signal descriptors of 8086
Signal descriptors of 8086
 
8086
80868086
8086
 
Introduction to Microprocessor
Introduction to MicroprocessorIntroduction to Microprocessor
Introduction to Microprocessor
 
Introduction to pic microcontroller
Introduction to pic microcontrollerIntroduction to pic microcontroller
Introduction to pic microcontroller
 
FreeRTOS
FreeRTOSFreeRTOS
FreeRTOS
 
PIN Specification of 8086 Microprocessor
PIN Specification of 8086 MicroprocessorPIN Specification of 8086 Microprocessor
PIN Specification of 8086 Microprocessor
 
Avr introduction
Avr introductionAvr introduction
Avr introduction
 
ICDL MODULE 1 Lesson 2
ICDL MODULE 1 Lesson 2ICDL MODULE 1 Lesson 2
ICDL MODULE 1 Lesson 2
 
Core I3 Vs Core I5
Core I3 Vs Core I5Core I3 Vs Core I5
Core I3 Vs Core I5
 
ARM Architecture Subroutine and Flags
ARM Architecture Subroutine and FlagsARM Architecture Subroutine and Flags
ARM Architecture Subroutine and Flags
 
80386 & 80486
80386 & 8048680386 & 80486
80386 & 80486
 
Pic microcontroller architecture
Pic microcontroller architecturePic microcontroller architecture
Pic microcontroller architecture
 
Intel+80286
Intel+80286Intel+80286
Intel+80286
 
8255 PPI
8255 PPI8255 PPI
8255 PPI
 
Mips architecture
Mips architectureMips architecture
Mips architecture
 
DDR2 SDRAM
DDR2 SDRAMDDR2 SDRAM
DDR2 SDRAM
 
4.programmable dma controller 8257
4.programmable dma controller 82574.programmable dma controller 8257
4.programmable dma controller 8257
 
8086 Micro-processor and MDA 8086 Trainer Kit
8086 Micro-processor and MDA 8086 Trainer Kit8086 Micro-processor and MDA 8086 Trainer Kit
8086 Micro-processor and MDA 8086 Trainer Kit
 
Introduction to Digital Signal processors
Introduction to Digital Signal processorsIntroduction to Digital Signal processors
Introduction to Digital Signal processors
 

Ähnlich wie Mips1

MIPS Merge Sort
MIPS Merge SortMIPS Merge Sort
MIPS Merge Sort
Adam Tucker
 
Hop ngu MIP
Hop ngu MIPHop ngu MIP
Hop ngu MIP
satlove02
 
Finish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdfFinish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdf
fasttrackcomputersol
 
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docxcs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
mydrynan
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
kayalkarnan
 
13BranchLoops.pptx
13BranchLoops.pptx13BranchLoops.pptx
13BranchLoops.pptx
MaFeCastro22
 
C:\Fakepath\Chapter 2 Part2 B
C:\Fakepath\Chapter 2 Part2 BC:\Fakepath\Chapter 2 Part2 B
C:\Fakepath\Chapter 2 Part2 B
ececourse
 

Ähnlich wie Mips1 (20)

MIPS Merge Sort
MIPS Merge SortMIPS Merge Sort
MIPS Merge Sort
 
Hop ngu MIP
Hop ngu MIPHop ngu MIP
Hop ngu MIP
 
What is the Insertion Sort MIPS Assembly codeSolution.globl m.pdf
What is the Insertion Sort MIPS Assembly codeSolution.globl m.pdfWhat is the Insertion Sort MIPS Assembly codeSolution.globl m.pdf
What is the Insertion Sort MIPS Assembly codeSolution.globl m.pdf
 
Mips
MipsMips
Mips
 
Finish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdfFinish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdf
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
 
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docxcs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
 
Data Types Master
Data Types MasterData Types Master
Data Types Master
 
Space Invaders Source Code
Space Invaders Source CodeSpace Invaders Source Code
Space Invaders Source Code
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
 
13BranchLoops.pptx
13BranchLoops.pptx13BranchLoops.pptx
13BranchLoops.pptx
 
C:\Fakepath\Chapter 2 Part2 B
C:\Fakepath\Chapter 2 Part2 BC:\Fakepath\Chapter 2 Part2 B
C:\Fakepath\Chapter 2 Part2 B
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
 
Instruction Set Architecture: MIPS
Instruction Set Architecture: MIPSInstruction Set Architecture: MIPS
Instruction Set Architecture: MIPS
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Creating a compiler in Perl 6
Creating a compiler in Perl 6Creating a compiler in Perl 6
Creating a compiler in Perl 6
 
Functional perl
Functional perlFunctional perl
Functional perl
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 

Mehr von Stefano Salvatori (20)

Salida a VolcĂĄn Copahue Febrero 2011
Salida a VolcĂĄn Copahue Febrero 2011Salida a VolcĂĄn Copahue Febrero 2011
Salida a VolcĂĄn Copahue Febrero 2011
 
Salida a Aguas Calientes abril 2011
Salida a Aguas Calientes abril 2011Salida a Aguas Calientes abril 2011
Salida a Aguas Calientes abril 2011
 
Sistema de capas
Sistema de capasSistema de capas
Sistema de capas
 
Enumeracion de Goedel
Enumeracion de GoedelEnumeracion de Goedel
Enumeracion de Goedel
 
Maquinas Abstractas
Maquinas AbstractasMaquinas Abstractas
Maquinas Abstractas
 
Intro
IntroIntro
Intro
 
Pipelining
PipeliningPipelining
Pipelining
 
Memoria I I
Memoria  I IMemoria  I I
Memoria I I
 
I S A
I S AI S A
I S A
 
C P U Uniciclo
C P U  UnicicloC P U  Uniciclo
C P U Uniciclo
 
Exc Int
Exc IntExc Int
Exc Int
 
DesempeñO
DesempeñODesempeñO
DesempeñO
 
Memoria I
Memoria  IMemoria  I
Memoria I
 
Codificacion
CodificacionCodificacion
Codificacion
 
Multiciclo
MulticicloMulticiclo
Multiciclo
 
Hebras
HebrasHebras
Hebras
 
Semaforos
SemaforosSemaforos
Semaforos
 
Planificacion Procesos Gral
Planificacion Procesos GralPlanificacion Procesos Gral
Planificacion Procesos Gral
 
Memoria Soporte Hw
Memoria Soporte HwMemoria Soporte Hw
Memoria Soporte Hw
 
Hebras En Accion
Hebras En AccionHebras En Accion
Hebras En Accion
 

KĂŒrzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

KĂŒrzlich hochgeladen (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Mips1

  • 1. MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker
  • 2.
  • 3.
  • 4. Hello World! .text # code section .globl main main: li $v0, 4 # system call for print string la $a0, str # load address of string to print syscall # print the string li $v0, 10 # system call for exit syscall # exit .data str: .asciiz “Hello world!” # NUL terminated string, as in C
  • 5. Addressing modes lw $s1, addr # load $s1 from addr lw $s1, 8($s0) # $s1 = Mem[$s0+8] register $s0 contains the base address access the address ($s0) possibly add an offset 8($s0)
  • 6. Load and move instructions la $a0, addr # load address addr into $a0 li $a0, 12 # load immediate $a0 = 12 lb $a0, c($s1) # load byte $a0 = Mem[$s1+c] lh $a0, c($s1) # load half word lw $a0, c($s1) # load word move $s0, $s1 # $s0 = $s1
  • 7.
  • 8. Branch instructions beqz $s0, label if $s0==0 goto label bnez $s0, label if $s0!=0 goto label bge $s0, $s1, label if $s0>=$s1 goto label ble $s0, $s1, label if $s0<=$s1 goto label blt $s0, $s1, label if $s0<$s1 goto label beq $s0, $s1, label if $s0==$s1 goto label bgez $s0, $s1, label if $s0>=0 goto label
  • 9. if-then-else structures if ($t0==$t1) then /* blockA */ else /* blockB */ beq $t0, $t1, blockA j blockB blockA: 
 instructions of then block 
 j exit blockB: 
 instructions of else block 
 exit: 
 subsequent instructions 

  • 10. repeat-until loop repeat 
 until $t0>$t1 loop: 
 instructions of loop 
 ble $t0, $t1, loop # if $t0<=$t1 goto loop Other loop structures are similar
 Exercise: Derive templates for various loop structures
  • 11.
  • 12. SPIM system calls $a0 address of string 4 print string $f12 contains number 3 print double $f12 contains number 2 print float $a0 contains number 1 print int argument code $v0 procedure
  • 13. SPIM system calls 8 read string res returned in $f0 7 read double res returned in $f0 6 read float res returned in $v0 5 read int result code $v0 procedure
  • 14.
  • 15. Print numbers 1 to 10 main: li $s0, 1 # $s0 = loop counter li $s1, 10 # $s1 = upper bound of loop loop: move $a0, $s0 # print loop counter $s0 li $v0, 1 syscall li $v0, 4 # print “” la $a0, linebrk # linebrk: .asciiz “” syscall addi $s0, $s0, 1 # increase counter by 1 ble $s0, $s1, loop # if ($s0<=$s1) goto loop li $v0, 10 # exit syscall
  • 16. Increase array elements by 5 .text .globl main main: la $t0, Aaddr # $t0 = pointer to array A lw $t1, len # $t1 = length (of array A) sll $t1, $t1, 2 # $t1 = 4*length add $t1, $t1, $t0 # $t1 = address(A)+4*length loop: lw $t2, 0($t0) # $t2 = A[i] addi $t2, $t2, 5 # $t2 = $t2 + 5 sw $t2, 0($t0) # A[i] = $t2 addi $t0, $t0, 4 # i = i+1 bne $t0, $t1, loop # if $t0<$t1 goto loop .data Aaddr: .word 0,2,1,4,5 # array with 5 elements len: .word 5
  • 17. Increase array elements by 5 .text .globl main main: la $t0, Aaddr # $t0 = pointer to array A lw $t1, len # $t1 = length (of array A) sll $t1, $t1, 2 # $t1 = 4*length (byte addr.) add $t1, $t1, $t0 # $t1 = beyond last elem. A
  • 18. Increase array elements by 5 Loop: lw $t2, ($t0) # $t2 = A[i] addi $t2, $t2, 5 # $t2 = $t2 + 5 sw $t2, ($t0) # A[i] = $t2 addi $t0, $t0, 4 # i = i+1 bne $t0, $t1, loop # if $t0<$t1 goto loop li $v0, 10 # exit syscall
  • 19. Increase array elements by 5 .data Aaddr: .word 0,2,1,4,5 len: .word 5 Idiosyncratic: Byte addressing => loop in steps of 4 Describe meaning of registers in your documentation!
  • 20.
  • 21.
  • 22. Write your own procedures # prints the integer contained in $a0 print_int: li $v0, 1 # system call to syscall # print integer jr $ra # return main: . . . li $a0, 10 # we want to print 10 jal print_int # print integer in $a0
  • 23. Write your own procedures .data linebrk: .asciiz “” .text print_eol: # prints &quot;&quot; li $v0, 4 # la $a0, linebrk # syscall # jr $ra # return main: . . . jal print_eol # printf(“”)
  • 24. Write your own procedures .data main: li $s0, 1 # $s0 = loop ctr li $s1, 10 # $s1 = upperbnd loop: move $a0, $s0 # print loop ctr jal print_int # jal print_eol # print &quot;&quot; addi $s0, $s0, 1 # loop ctr +1 ble $s0, $s1, loop # unless $s0>$s1

  • 25.
  • 26.
  • 27. Fibonacci fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) 0, 1, 1, 2, 3, 5, 8, 13, 21,

  • 28. Fibonacci li $a0, 10 # call fib(10) jal fib # move $s0, $v0 # $s0 = fib(10) fib is a recursive procedure with one argument $a0 need to store argument $a0, temporary register $s0 for intermediate results, and return address $ra
  • 29. fib: sub $sp,$sp,12 # save registers on stack sw $a0, 0($sp) # save $a0 = n sw $s0, 4($sp) # save $s0 sw $ra, 8($sp) # save return address $ra bgt $a0,1, gen # if n>1 then goto generic case move $v0,$a0 # output = input if n=0 or n=1 j rreg # goto restore registers gen: sub $a0,$a0,1 # param = n-1 jal fib # compute fib(n-1) move $s0,$v0 # save fib(n-1) sub $a0,$a0,1 # set param to n-2 jal fib # and make recursive call add $v0, $v0, $s0 # $v0 = fib(n-2)+fib(n-1) rreg: lw $a0, 0($sp) # restore registers from stack lw $s0, 4($sp) # lw $ra, 8($sp) # add $sp, $sp, 12 # decrease the stack size jr $ra
  • 30.