DLX汇编例程
例程一
;add3.s
; Program begin at symbol main
; requires module INPUT
; Load two programs (add3.s and input.s)
; Note: load add.s first then follow by inputs.
; Execute, press (F5)
; read 1 numbers from stdin and add 3 upon it (integer)
; the result is written to stdout
;--------------------------------------------------------------------------
.data
Prompt: .asciiz "\nEnter the number(> 1): "
.align 2
StrPtr: .space 4
PrintFt: .asciiz "Your number %d+3 = %d"
.align 2
PrintPar: .word PrintFt
Yournumb: .space 4
PrintSum: .space 4
.text
.global main
main:
addi r1,r0,Prompt ;put prompt in arg r1
jal InputUnsigned ;call subroutine
sw Yournumb,r1 ;store your number
addi r2,r1,3 ;add 3 to your number
sw PrintSum,r2 ;store sum
addi r14,r0,PrintPar ;prepare to print
trap 5 ;print to standard output
;**********end************
trap 0
例程二
;
; getchar.s
;
; R1 = getchar();
.data
ReadBuf: .space 8
ReadPar: .word 0,ReadBuf,4
SaveR14: .space 4
SaveR15: .space 4
.text
.global getchar
getchar:
sw SaveR14, r14 ;save register r14
sw SaveR14, r15 ;save register r15
addi r14,r0,ReadPar
trap 3
addi r15,r0,ReadBuf
lbu r1,0(r15)
lw r14, SaveR14
lw r15, SaveR15
jr r31 ;return
例程三
;
; ascii.s
;--------------------------------------------------------------------------
; Program begin at symbol main
; requires module getchar
; print the character ascii code
;--------------------------------------------------------------------------
.data
Hello: .asciiz "\n\nEnter any char(hit return after that,
q to quit): "
.align 2
StrPtr: .space 4
PrintFt: .asciiz "\nThis char = %c and its ascii value =
%d"
.align 2
PrintPar: .word PrintFt
PrintChar: .space 4
PrintVal: .space 4
.text
.global main
main:
addi r2,r0,Hello
sw StrPtr,r2
addi r14,r0,StrPtr
trap 5
jal getchar
seqi r3,r1,113
sw PrintChar(R0),R1
sw PrintVal(R0),R1
addi r14,r0,PrintPar
trap 5
beqz r3,main
;*** end
trap 0