.data m: .word 100 array: .space 0x200 .text main: sub $sp, $sp, 12 sw $fp, 4($sp) add $fp, $sp, 12 sw $ra, 0($fp) li $t0, 0 sw $t0, -4($fp) loop: lw $t0, -4($fp) lw $t1, m bge $t0,$t1,loop_done move $a0, $t0 jal gauss lw $t0, -4($fp) sll $t2, $t0, 2 sw $v0,array($t2) addi $t0,1 sw $t0, -4($fp) j loop loop_done: lw $ra, 0($fp) lw $fp, -8($fp) add $sp, $sp, 12 jr $ra gauss: sub $sp, $sp, 8 sw $fp, 4($sp) add $fp, $sp, 8 sw $ra, 0($fp) li $v0, 0 li $t0, 1 gloop: bgt $t0, $a0, gloop_done add $v0, $v0, $t0 addi $t0, 1 j gloop gloop_done: lw $ra, 0($fp) lw $fp, -4($fp) add $sp, $sp, 8 jr $raThis is a straightforward translation of the OIC program. It is also available as ../public/gauss.mips. You should try running it with the xspim simulator. An improved version would use the mult instruction instead:
gauss: addiu $t0, $a0, 1 mul $v0, $t0, $a0 sra $v0, $v0, 1 jr $ra
BTW, I was hoping that one of you would notice this, but there is a more efficient way to do array indexing in the one-instruction computer. The complete replacement for subr-main follows:
; ; main program. calls subr-sum, with inputs 0..M-1 ; results placed in locations 0x300..(0x300+M-1) ; ; M is stored in location 0 ; ; assume standard macros (see subr-sum.masm) are ; available ; M .equ 0 array .equ 0x0300 gauss .equ 0x0100 ret .equ 0x0108 move: .macro src,dst subge tmp,tmp,next subge tmp,src,next subge dst,dst,next subge dst,tmp,next .endmacro inc: .macro var subge var,neg1,next .endmacro .data neg1: .word -1 .text zero: .macro var subge var,var,next .endmacro goto: .macro label subge tmp,tmp,label .endmacro call: .macro subr,retInstAddr zero retInstrAddr subge retInstrAddr,L0,next goto subr LretLabel: .data ; indicate place elsewhere, not ; part of the instruction stream L0: .word neg(triple(tmp,tmp,LretLabel)) ; L label means local label .text .endmacro .org 0x0200 zero i .data negClearInstr: .word neg(triple(array,array,clearbuf+1)) negXferInstr: .word neg(triple(array,tmp,xferbuf+1)) bumpClear: .word neg(triple(1,1,0)) bumpXfer: .word neg(triple(1,0,0)) .text zero clearbuf subge clearbuf,negClearInstr,next zero xferbuf subge xferbuf,negXferInstr,next loop: move i,N call gauss,ret subge tmp,tmp,next subge tmp,sum,next clearbuf: subge 0,0,0 ; placeholder xferbuf: subge 0,0,0 ; placeholder subge clearbuf,bumpClear,next subge xferbuf,bumpXfer,next inc i subge tmp,tmp,next subge tmp,M,next subge tmp2,tmp2,next subge tmp2,tmp,next subge tmp2,i,loop endlabel: subge tmp,tmp,endlabel ;end .data i: .word 0 tmp: .word 0 tmp2: .word 0
bsy+www@cs.ucsd.edu, last updated