190 likes | 359 Views
MIPS Assembly Language Programming. CDA 3101 Discussion Session 04. 1. Outline. MIPS simulator – PCSpim Installation Try it with small programs !. 2. PCSpim. Installation From the textbook CD From the internet. http://www.cs.wisc.edu/~larus/spim.html. 3. Writing A Basic Program.
E N D
MIPS Assembly Language Programming CDA 3101 Discussion Session 04 1
Outline • MIPS simulator – PCSpim • Installation • Try it with small programs ! 2
PCSpim • Installation • From the textbook CD • From the internet • http://www.cs.wisc.edu/~larus/spim.html 3
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
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
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
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
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
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
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
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); li $v0,4 la $a0,disp2 syscall li $v0,1 move $a0,$t0 syscall li $v0,4 la $a0,disp3 syscall li $v0,10 syscall 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
PC Spim 12
PC Spim • Note the top window – it contains the state of all registers. 13
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
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
PC Spim • Click this menu item to change settings for the emulator. 16
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
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