Title: PCSpim
1PCSpim
2(No Transcript)
3- This is the place where you keep any constants
in your program
- Error messages
- Prompt messages for user input
- Any constants, such as 3.14
- Input/Output buffers (contents vary, but buffer
size unchanged)
- The data section is declared with the .data
assembler directive
- SPIM assumes the data section is at the beginning
- Because the program code is supposed to be at
the end
PCSpim
4- This is the place where you declare your
assembly program
- The program definition section is declared with
the .text assembler directive
- The beginning label of your assembly program
declared - with .globl name_of_your_beginning_label
? The program definition section should be the
simplest (and shortest)
PCSpim
5- This is the place where you write your program
(assembly instructions)
- You must start with the beginning label you
declared
- If you declare .globl main in program
definition
- You must start your program with main label
- Your program should end with a jr 31
instruction
? If you forget the jr 31 instruction, the CPU
continues to execute
What instructions will be executed?
PCSpim
6Template
7PCSpim
8main jr 31 Stop program
PCSpim
9PCSpim
10PCSpim
11PCSpim
12PCSpim
13PCSpim
14PCSpim
15Most probably, the least important window ...
PCSpim
16PCSpim
17PCSpim
18PCSpim
19System Calls
20(No Transcript)
21Our First Program
22(No Transcript)
23Sum of Integers entered
main() prompt user for input read input number as
a string into numString write output sum
message index 0 number 0 while (TRUE)
char numStringindex if ((char linefeed)
AND (char NULL)) then number number
(char AND 0xF) else goto exit_loop end
if index index 1 end while exit_loop output
sum output newline exit end main
2423 Code segment
24 .text 25 .globl
main 26 main 27 la a0,number_prompt prompt
user for input 28 li v0,4 29 syscall 30 31
la a0,number read the input number 32 li
a1,11 as a string 33 li v0,8 34
syscall 35 36 la a0,out_msg write output
message 37 li v0,4 38 syscall 39 40 la
t0,number pointer to number 41 li t2,0
init sum to zero 42 loop 43 lb t1,(t0) 44
beq t1,0xA,exit_loop if linefeed or 45 beqz
t1,exit_loop NULL, we are done 46 and
t1,t1,0x0F strip off upper 4 bits 47 addu
t2,t2,t1 add to running total 48 addu
t0,t0,1 increment pointer 49 b loop 50
exit_loop 51 move a0,t2 output sum 52 li
v0,1 53 syscall 54 55 la a0,newline output
newline 56 li v0,4 57 syscall 58 59 li
v0,10 exit 60 syscall
1 Add individual digits of a number
ADDIGITS.ASM 2 3 Objective To add
individual digits of an integer. 4 The number
is read as a string. 5 Input Requests a
number from the user. 6 Output Outputs the
sum. 7 8 t0 - points to input string
(i.e., input number) 9 t1 - holds a digit for
processing 10 t2 - maintains the running
total 11 12 Data segment
13 .data 14
number_prompt 15 .asciiz "Please enter a number
(lt11 digits) " 16 out_msg 17 .asciiz "\nThe
sum of individual digits is " 18 newline 19
.asciiz "\n" 20 number 21 .space 11 space
for input string 22