体系结构试验--DLX指令集--myFACT.…

来源:互联网 发布:淘宝卖足球鞋店铺排名 编辑:程序博客网 时间:2024/05/18 00:00
;--------------------------------------------------------------------------
; Program begin at symbol main
; requires module INPUT
; read a number from stdin and calculate the factorial (type:double)
; the result is written to stdout
;--------------------------------------------------------------------------

.data
Prompt: .asciiz "An integer value >1 : "
PrintfFormat: .asciiz "Factorial = %g\n\n";输出格式,表示求出的阶乘按什么格式输出
.align 2  ;表示下面采用字对齐
PrintfPar: .word PrintfFormat
PrintfValue: .space 8

.text ;第一代码段,默认情况下代码段$CODE会加载到内存0x100地址处.global main ;即该代码段的首地址
main:
addi r1,r0,Prompt ;Read valuefrom stdin into R1
jal InputUnsigned ;跳转到InputUnsigned子程序处读取输入的参数,同时将一条指令的地址存与r31中,即r31<-pc+4
              addd f2,f2,f0
              addd f4,f4,f6
;*** init values
movi2fp f10,r1 ;R1 -> D0 D0..Count register
cvti2d f0,f10
addi r2,r0,1 ;1 -> D2 D2..result
movi2fp f11,r2
cvti2d f2,f11
movd f4,f2 ;1-> D4 D4..Constant 1
;*** Break loop if D0 = 1
Loop: led f0,f4 ;D0<=1 ?
bfpt Finish
;*** Multiplication and next loop
multd f2,f2,f0
subd f0,f0,f4
              subd          f0,f0,f4
j Loop

Finish: ;*** write result to stdout
sd PrintfValue,f2
addi r14,r0,PrintfPar
trap 5
;*** end
trap 0

原创粉丝点击