Download presentation
Presentation is loading. Please wait.
1
MIPS Assembly Language Programming
CDA 3101 Discussion Session 04 1 1 1
2
Outline MIPS simulator – PCSpim Installation
Try it with small programs ! 2 2 2
3
Installation PCSpim From the textbook CD From the internet
3 3 3
4
Writing A Basic Program
Let’s start by writing a program that sums all the numbers between 1 and 5. void main() { printf("Test Program #1\n"); int sum = 0; int i = 5; do sum = sum + i; i = i - 1; } while( i > 0 ); printf("Sum from 1 to 5 = %d",sum); printf("\nEnd of Program\n"); } 4 4 4
5
Writing A Basic Program
Summing numbers between 1 and 5. Something… like this: void main() { printf("Test Program #1\n"); int sum = 0; int i = 5; do sum = sum + i; i = i - 1; } while( i > 0 ); printf("Sum from 1 to 5 = %d",sum); printf("\nEnd of Program\n"); } 5 5 5
6
Writing A Basic Program
How can we translate this into MIPS? void main() { printf("Test Program #1\n"); int sum = 0; int i = 5; do sum = sum + i; i = i - 1; } while( i > 0 ); printf("Sum from 1 to 5 = %d",sum); printf("\nEnd of Program\n"); } 6 6 6
7
Writing A Basic Program
How can we translate this into MIPS? void main() { printf("Test Program #1\n"); int sum = 0; int i = 5; do sum = sum + i; i = i - 1; } while( i > 0 ); printf("Sum from 1 to 5 = %d",sum); printf("\nEnd of Program\n"); } # Our first test program for MIPS Simulator (pcSpim) .data .disp1: .asciiz “Test Program #1\n” .disp2: .asciiz “Sum from 1 to 5 = “ .disp3: .asciiz “\nEnd of program\n .text #Tells us this is the code section. .globl main #Tells compiler that this is a #public location (function) main: … #The start of the function 7 7 7
8
Writing A Basic Program
How can we translate this into MIPS? void main() { printf("Test Program #1\n"); int sum = 0; int i = 5; do sum = sum + i; i = i - 1; } while( i > 0 ); printf("Sum from 1 to 5 = %d",sum); printf("\nEnd of Program\n"); } … main: li $t0, 0 #int sum = 0; li $v0, 5 #int i = 5; So we will be using $t0 as sum, and $v0 as i 8 8 8
9
Writing A Basic Program
How can we translate this into MIPS? … main: li $t0, 0 #int sum = 0; li $v0, 5 #int i = 5; #do loop: … bgtz $v0, loop #while(i > 0); To make a do … while loop, we will need a label to jump to. It’s really the old goto function. void main() { printf("Test Program #1\n"); int sum = 0; int i = 5; do sum = sum + i; i = i - 1; } while( i > 0 ); printf("Sum from 1 to 5 = %d",sum); printf("\nEnd of Program\n"); } 9 9 9
10
Writing A Basic Program
How can we translate this into MIPS? void main() { printf("Test Program #1\n"); int sum = 0; int i = 5; do sum = sum + i; i = i - 1; } while( i > 0 ); printf("Sum from 1 to 5 = %d",sum); printf("\nEnd of Program\n"); } … main: li $t0, 0 #int sum = 0; li $v0, 5 #int i = 5; #do loop: add $t0, $t0, $v0 #sum = sum + i; addi $v0, $v0, -1 #i = i – 1; bgtz $v0, loop #while(i > 0); Now we can perform the real work. 10 10 10
11
How can we translate this into MIPS?
main: li $v0,4 la $a0,disp1 syscall li $t0, 0 #int sum = 0; li $v0, 5 #int i = 5; loop: add $t0, $t0, $v0 #sum = sum + i; addi $v0, $v0, -1 #i = i – 1; bgtz $v0, loop #while(i > 0); la $a0,disp2 li $v0,1 move $a0,$t0 la $a0,disp3 li $v0,10 void main() { printf("Test Program #1\n"); int sum = 0; int i = 5; do sum = sum + i; i = i - 1; } while( i > 0 ); printf("Sum from 1 to 5 = %d",sum); printf("\nEnd of Program\n"); } 11 11 11
12
PC Spim 12 12 12
13
PC Spim Note the top window – it contains the state of all registers.
13 13 13
14
PC Spim The button on the top right runs the program to its end, after you click “OK” on the dialog box. Note that you won’t see the register changes until the program ends. 14 14 14
15
PC Spim Click this menu item to reinitialize PC Spim – it’s like rebooting your computer. It’s often necessary to click Reload to run your program again. 15 15 15
16
PC Spim Click this menu item to change settings for the emulator. 16
17
PC Spim Click this menu item to change settings for the emulator.
Sometimes it’s helpful to uncheck “General registers in hexadecimal” so that you can read values as regular numbers. 17 17 17
18
PC Spim Click the button that looks like a hand to set breakpoints. The program will stop running at positions you indicate, and wait for your authorization to continue upon reaching said point. You will also see the register values updated. 18 18 18
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.