[sample program 1]
# program reads integers and adds them in the loop,
# it stops when 0 is read, printing out the sum at that point
main: # begin of assembly program consisting only of text segment
li $a0, 0 # pseudoinstruction: move immediate to dest
loop:
li $v0, 5 # system call read integer code in $v0
syscall # system call: feature of SPIM, not MIPS
add $a0, $a0, $v0 # in $v0 read integer
bne $v0, $zero, loop # if not 0 back to loop
li $v0, 1 # print integer system code to $v0
syscall # proper printing
li $v0, 10 # exit program system code in $v0
syscall # exit program
[sample program 2]
.text # begin of code
.globl __start
__start:
li $a0, 0 # $a0라는 아큐먼트레지스터에 constant값 0을 대입합니다.
loop:
li $v0, 5 # $v0는 시스템이 어떤 일을 할지 정해주는 레지스터입니다. 여기서 5라 함은 시스템이 입력값을 받을수 있도록 기다리라는 뜻입니다.
syscall #이것은 spim 문법으로 이것이 호출되면 $v0에 있는 내용에 따라 시스템이 작동합니다.
add $a0, $a0, $v0 # 어떤 입력값이 들어오면 그것을 $a0에 더합니다. (여기서 $v0는 시스템의 입력값을 가지는 리턴값이라고 생각하시면 됩니다)
bne $v0, $zero, loop # 만약 들어온 값이 숫자이면 계속 loop로 가서 루프를 돕니다.
li $v0, 1 #숫자가 아니거나 숫자 0이면 $v0를 1로 셋팅함으로서 print integer 명령을 주고
syscall # 실행합니다. 이 때 화면에 찍히는 값은 $a0에 있는 값입니다.
li $v0, 10 # $v0를 10으로 셋팅함으로써 종료 명령을 셋팅하고
syscall # 실행시킴으로써 프로그램을 종료합니다.
[sample program 3]
# C program
# #include
# main()
# {
# int i;
# int sum=0;
# for (i=0; i<=100; i=i+1) sum=sum + i*i;
# printf("The sum from 0 .. 100 is %d\n",sum);
# }
.text # 프로그램시작
.align 2 # 1word를 2bytes로 셋팅
.globl main # main함수 선언
main:
li $t0,0 # clear $t0 - i=0
li $t2,0 # clear sum
loop:
mul $t1,$t0,$t0 # i * i
addu $t2,$t2,$t1 # calculate new sum
addi $t0,$t0,1 # i = i + 1
ble $t0,100,loop # if i<=100 return to loop
la $a0,str # address of string to print in $a0
li $v0,4 # system call code for print_str SPIM i/o call
syscall
move $a0,$t2 # pseudoinstr: move result to $a0
li $v0,1 # system call code for print_int
syscall
jr $ra # end of program, return to system
.data # put this to data segment
str: # string for printing
.asciiz "The sum from 0 .. 100 is "
[sample program 4]
# 10! 구하는 프로그램~
#
# main ()
# {
# printf("The factorial of 10 is %d\n",fact(10));
# }
#
# int fact(int n)
# {
# if (n<1)
# return(1);
# else
# return(n*fact(n-1));
# }
.text # put code into text segment
.globl __start # declare label main global
# to be accessible from other files
__start:
subu $sp,$sp,32 # stack frame is 32 bytes long,
# min length 24
# here we use it to store only
# $fp, $ra, and $a0
sw $fp,16($sp) # save old frame pointer, after $a0-$a3
sw $ra,20($sp) # save return address in $ra
addu $fp,$sp,28 # set up frame pointer to the 1st word in frame
li $a0,10 # put argument (10) for factorial
jal fact # call factorial function
move $s0,$v0 # store result from $v0 to avoid destruction
la $a0,$LC # put string address in $a0
li $v0,4 # system call code for print_str
syscall
move $a0,$s0 # pseudoinstr: move result to $a1
li $v0,1 # system call code for print_int
syscall
lw $ra,20($sp) # restore return address
lw $fp,16($sp) # restore frame pointer
addu $sp,$sp,32 # pop stack frame - clean frame
jr $ra # end of program, return to system
.rdata # put this to data segment
$LC: # string for printing
.asciiz "The factorial of 10 is \n"
.text # again text segment
fact: # keeps code for factorial
subu $sp,$sp,32 # same thing with frame preparation
sw $ra,20($sp)
sw $fp,16($sp)
addu $fp,$sp,28
sw $a0,0($fp) # save factorial argument (n)
lw $v0,0($fp) # load n - kept in $a0 saved on stack
bgtz $v0,$L2 # branch if n>0
li $v0,1 # return 1 (0!=1)
j $L1 # jump to code to return
$L2:
lw $v1,0($fp) # load n
subu $v0,$v1,1 # compute n-1
move $a0,$v0 # move value to $a0
jal fact # call recursively factorial function
lw $v1,0($fp) # load n, here in fact proper computation of factorial
mul $v0,$v0,$v1 # computer fact(n-1) * n, note that $v0 is preserved
# it multiplies successive values from $a0 stored in frames
$L1: # result is in $v0
lw $ra,20($sp) # restore $ra
lw $fp,16($sp) # restore $fp
addu $sp,$sp,32 # pop stack
j $ra # return to caller
# program reads integers and adds them in the loop,
# it stops when 0 is read, printing out the sum at that point
main: # begin of assembly program consisting only of text segment
li $a0, 0 # pseudoinstruction: move immediate to dest
loop:
li $v0, 5 # system call read integer code in $v0
syscall # system call: feature of SPIM, not MIPS
add $a0, $a0, $v0 # in $v0 read integer
bne $v0, $zero, loop # if not 0 back to loop
li $v0, 1 # print integer system code to $v0
syscall # proper printing
li $v0, 10 # exit program system code in $v0
syscall # exit program
[sample program 2]
.text # begin of code
.globl __start
__start:
li $a0, 0 # $a0라는 아큐먼트레지스터에 constant값 0을 대입합니다.
loop:
li $v0, 5 # $v0는 시스템이 어떤 일을 할지 정해주는 레지스터입니다. 여기서 5라 함은 시스템이 입력값을 받을수 있도록 기다리라는 뜻입니다.
syscall #이것은 spim 문법으로 이것이 호출되면 $v0에 있는 내용에 따라 시스템이 작동합니다.
add $a0, $a0, $v0 # 어떤 입력값이 들어오면 그것을 $a0에 더합니다. (여기서 $v0는 시스템의 입력값을 가지는 리턴값이라고 생각하시면 됩니다)
bne $v0, $zero, loop # 만약 들어온 값이 숫자이면 계속 loop로 가서 루프를 돕니다.
li $v0, 1 #숫자가 아니거나 숫자 0이면 $v0를 1로 셋팅함으로서 print integer 명령을 주고
syscall # 실행합니다. 이 때 화면에 찍히는 값은 $a0에 있는 값입니다.
li $v0, 10 # $v0를 10으로 셋팅함으로써 종료 명령을 셋팅하고
syscall # 실행시킴으로써 프로그램을 종료합니다.
[sample program 3]
# C program
# #include
# main()
# {
# int i;
# int sum=0;
# for (i=0; i<=100; i=i+1) sum=sum + i*i;
# printf("The sum from 0 .. 100 is %d\n",sum);
# }
.text # 프로그램시작
.align 2 # 1word를 2bytes로 셋팅
.globl main # main함수 선언
main:
li $t0,0 # clear $t0 - i=0
li $t2,0 # clear sum
loop:
mul $t1,$t0,$t0 # i * i
addu $t2,$t2,$t1 # calculate new sum
addi $t0,$t0,1 # i = i + 1
ble $t0,100,loop # if i<=100 return to loop
la $a0,str # address of string to print in $a0
li $v0,4 # system call code for print_str SPIM i/o call
syscall
move $a0,$t2 # pseudoinstr: move result to $a0
li $v0,1 # system call code for print_int
syscall
jr $ra # end of program, return to system
.data # put this to data segment
str: # string for printing
.asciiz "The sum from 0 .. 100 is "
[sample program 4]
# 10! 구하는 프로그램~
#
# main ()
# {
# printf("The factorial of 10 is %d\n",fact(10));
# }
#
# int fact(int n)
# {
# if (n<1)
# return(1);
# else
# return(n*fact(n-1));
# }
.text # put code into text segment
.globl __start # declare label main global
# to be accessible from other files
__start:
subu $sp,$sp,32 # stack frame is 32 bytes long,
# min length 24
# here we use it to store only
# $fp, $ra, and $a0
sw $fp,16($sp) # save old frame pointer, after $a0-$a3
sw $ra,20($sp) # save return address in $ra
addu $fp,$sp,28 # set up frame pointer to the 1st word in frame
li $a0,10 # put argument (10) for factorial
jal fact # call factorial function
move $s0,$v0 # store result from $v0 to avoid destruction
la $a0,$LC # put string address in $a0
li $v0,4 # system call code for print_str
syscall
move $a0,$s0 # pseudoinstr: move result to $a1
li $v0,1 # system call code for print_int
syscall
lw $ra,20($sp) # restore return address
lw $fp,16($sp) # restore frame pointer
addu $sp,$sp,32 # pop stack frame - clean frame
jr $ra # end of program, return to system
.rdata # put this to data segment
$LC: # string for printing
.asciiz "The factorial of 10 is \n"
.text # again text segment
fact: # keeps code for factorial
subu $sp,$sp,32 # same thing with frame preparation
sw $ra,20($sp)
sw $fp,16($sp)
addu $fp,$sp,28
sw $a0,0($fp) # save factorial argument (n)
lw $v0,0($fp) # load n - kept in $a0 saved on stack
bgtz $v0,$L2 # branch if n>0
li $v0,1 # return 1 (0!=1)
j $L1 # jump to code to return
$L2:
lw $v1,0($fp) # load n
subu $v0,$v1,1 # compute n-1
move $a0,$v0 # move value to $a0
jal fact # call recursively factorial function
lw $v1,0($fp) # load n, here in fact proper computation of factorial
mul $v0,$v0,$v1 # computer fact(n-1) * n, note that $v0 is preserved
# it multiplies successive values from $a0 stored in frames
$L1: # result is in $v0
lw $ra,20($sp) # restore $ra
lw $fp,16($sp) # restore $fp
addu $sp,$sp,32 # pop stack
j $ra # return to caller