可以使用MARS来编汇编,MARS是一个用java编的IDE,它是一个模拟环境.
样例:重要的句子输出三遍
.data str: .asciiz "weidiao is great " .text j main f: la $a0,str li $v0,4 syscall jr $ra main: li $t1,0 li $t2,3 loop: bgt $t1,$t2,over addi $t1,$t1,1 jal f j loop over: li $v0,10 syscall
指令文档:链接
下面程序为一个四则运算程序,学习汇编就是要大量的阅读实例.实例学习在编程技术中至关重要.
#simple example :a small calculater’ .text # text section .globl main # call main by SPIM main: la $t0, value #t0 is the address of the value # load address "value" into $t0 la $a0,msg0 li $v0,4 syscall #print "please choose the operation:" li $v0, 5 syscall sw $v0, 8($t0) #operation is stored to t0+8---the second byte la $a0,msg1 #v0 controls the syscall.a0 controls the output_data; li $v0,4 syscall #print "first num:" li $v0, 5 #load integer 5 to v0,this is the read operation. syscall sw $v0, 0($t0) #store $v0 la $a0,msg2 li $v0,4 syscall #print " second num:" li $v0, 5 syscall sw $v0, 4($t0) #read the other num la $a0,newline li $v0,4 syscall #print "/n" lw $t1, 0($t0) # load the first num lw $t2, 4($t0) # load the second num lw $t3, 8($t0) # load the operation beq $t3,1,addOp # if + beq $t3,2,subOp # if - beq $t3,3,mulOp # if * beq $t3,4,divOp # if / addOp: add $t4, $t1, $t2 # $t1 + $t2 = $t4 sw $t4, 12($t0) # la $t5,addFlag j printResult subOp: sub $t4, $t1, $t2 # $t1 - $t2 = $t4 sw $t4, 12($t0) la $t5,subFlag j printResult mulOp: mul $t4, $t1, $t2 # $t1 * $t2 = $t4 sw $t4, 12($t0) la $t5,mulFlag j printResult divOp: div $t4, $t1, $t2 # $t1 / $t2 = $t4 sw $t4, 12($t0) la $t5,divFlag j printResult printResult: lw $a0,0($t0) li $v0,1 syscall #read first number la $a0,0($t5) li $v0,4 syscall #print opflag lw $a0,4($t0) li $v0,1 syscall #print second number la $a0,equalStr li $v0,4 syscall #print " = " lw $a0,12($t0) li $v0,1 syscall # print sum result j exit exit: la $a0,newline li $v0,4 syscall #print " /n " li $v0,10 syscall # exit # data section .data value: .word 0, 0, 0 ,0 ,0 # 0: first num ,4 : second num , 8 : operation , 12:result msg0 : .asciiz " please choose the operation(1~4):/n/t/t1 : +,addition /n/t/t2 : -,subtracter/n/t/t3 : * multiplication /n/t/t4 : /,division/n" msg1 : .asciiz "first num:" msg2 : .asciiz "second num:" addFlag : .asciiz " + " subFlag : .asciiz " - " mulFlag : .asciiz " * " divFlag : .asciiz " / " equalStr : .asciiz " = " newline : .asciiz "/n===============================/n"
样例:打印九九乘法表
.data op :.asciiz "*" eq:.asciiz "=" line:.asciiz " " space:.asciiz " " .text j main print: move $a0,$t1 li $v0,1 syscall la $a0,op li $v0,4 syscall move $a0,$t2 li $v0,1 syscall la $a0,eq li $v0,4 syscall mult $t1,$t2 mflo $a0 li $v0,1 syscall la $a0,space li $v0,4 syscall blt $t2,$t1,ret la $a0,line syscall ret: jr $ra main: li $t1,0 for1: addi $t1,$t1,1 bgt $t1,9,for1_out li $t2,0 for2: addi $t2,$t2,1 bgt $t2,$t1,for1 jal print j for2 j for1 for1_out: li $v0,10 syscall