现看的,可能不是太好,不过倒是能用-x-b
.data
str0: .asciiz "Primes in 1000:\n"
str1: .asciiz ",\n"
.text
.globl __start
__start:
la $a0, str0
li $v0, 4
syscall
li $t0, 2
j __test0
__l0:
li $t1, 1
li $t2, 2
j __test1
__l1:
div $t0, $t2
mfhi $t4
bne $t4, $0, __br1
move $t1, $0
__br1:
addi $t2, 1
__test1:
bne $t2, $t0, __l1
beq $t1, $0, __br0
move $a0, $t0
li $v0, 1
syscall
la $a0, str1
li $v0, 4
syscall
__br0:
addi $t0, 1
__test0:
li $t4, 100
bne $t0, $t4, __l0
li $v0, 10
syscall
----
有个事忘说了,这个真机上可能跑不了,那个模拟器模拟delay slot我没弄明白。
对于MIPS汇编语言的初学者,有什么样的教材推荐?
MIPS 汇编语言,最经典的教材叫 see Mips runs,网上有它的中文版。
dangdang上也有得卖,书名叫《MIPS体系结构透视》
如果你要学好MIPS 汇编,必须实验环境才能深入理解,特别是如果没有学过汇编语言,
会对寻址,操作数,立即数,寄存器很晕头的。
mips汇编求c(n,k)
代码如下:(这个是百度得到的)
~/viHello.c
"Hello.c"[Newfile]
/*Exampletoillustratemipsregisterconvention
*-Author:BNN
*11/29/2001
*/intaddFunc(int,int);
intsubFunc(int);voidmain()
{intx,y,z;
x=1;
y=2;
z=addFunc(x,y);
}
intaddFunc(intx,inty)
{
intvalue1=5;
intvalue2;value2=subFunc(value1);
return(x+y+value2);}intsubFunc(intvalue)
{
returnvalue--;
}
反汇编代码后的代码:
/*mainFunction*/
0000000000000000:
/*createastackframebymovingthestackpointer8
*bytesdownandmeantimeupdatethespvalue
*/
0:27bdfff8addiu$sp,$sp,-8
/*Savethereturnaddresstothecurrentspposition.*/
4:afbf0000sw$ra,0($sp)
8:0c000000jal0
/*nopisforthedelayslot*/
c:00000000nop
/*Filltheargumenta0withthevalue1*/
10:24040001li$a0,1
/*JumptheaddFunc*/
14:0c00000ajal28
/*NOTEHERE:Whywefillthesecondargument
*behindtheaddFuncfunctioncall?
*Thisisallaboutthe"-O1"compilationoptimizaiton.
*Withmipsarchitecture,theinstrucitonafterjump
*willalsobefetchedintothepiplineandget
*exectuted.Therefore,wecanpromisethatthe
*secondargumentwillbefilledwiththevalueof
*integer2.
*/
18:24050002li$a1,2
/*Loadthereturnaddressfromthestackpointer
*Noteherethattheresultv0containstheresultof
*addFuncfunctioncall
*/
1c:8fbf0000lw$ra,0($sp)
/*Return*/
20:03e00008jr$ra
/*Restorethestackframe*/
24:27bd0008addiu$sp,$sp,8/*addFuncFunction*/
0000000000000028:
/*Createastackframebyallocating16bytesor4
*wordssize
*/
28:27bdfff0addiu$sp,$sp,-16
/*Savethereturnaddressintothestackwith8bytes
*offset.Pleasenotethatcompilerdoesnotsavethe
*rato0($sp).
*Thinkofwhy,incontrastofthepreviousPowerPC
*EABIconvention
*/
2c:afbf0008sw$ra,8($sp)
/*Wesavethes1reg.valueintothestack
*becausewewilluses1inthisfunction
*Notethatthe4,5,6,7($sp)positionswillthen
*beoccupiedbythis32bitssizeregister
*/
30:afb10004sw$s1,4($sp)
/*Withesamereason,saves0reg.*/
34:afb00000sw$s0,0($sp)
/*Retrievetheargument0intos0reg.*/
38:0080802dmove$s0,$a0
/*Retrievetheargument1intos1reg.*/
3c:00a0882dmove$s1,$a1
/*CallthesubFuncwitha0with5*/
40:0c000019jal64
/*Inthedelayslot,weloadthe5intoargumenta0reg
*forsubFunccall.
*/
44:24040005li$a0,5
/*s0=s0+s1;notethats0ands1holdsthevaluesof
*x,y,respectively
*/
48:02118021addu$s0,$s0,$s1
/*v0=s0+v0;v0holdsthereturnresultsofsubFunc
*call;Andweletv0holdthefinalresults
*/
4c:02021021addu$v0,$s0,$v0
/*Retrievetheravaluefromstack*/
50:8fbf0008lw$ra,8($sp)
/*!!!!restorethes1reg.value*/
54:8fb10004lw$s1,4($sp)
/*!!!!restorethes0reg.value*/
58:8fb00000lw$s0,0($sp)
/*Returnbacktomainfunc*/
5c:03e00008jr$ra
/*Update/restorethestackpointer/frame*/
60:27bd0010addiu$sp,$sp,16/*subFuncFunction*/
0000000000000064:
/*returnbacktoaddFuncfunction*/
64:03e00008jr$ra
/*Takingadvantageofthemipsdelayslot,fillingthe
*resultregv0bysimplyassigningthev0asthevalue
*ofa0.Thisisabugfrommycsource
*codes--"value--".Ishouldwritemycodes
*like"--value",instead.
68:0080102dmove$v0,$a0
mips汇编 字符串
得到字符串长度,存入寄存器以计数,得到最后一个字符的地址,将最后一个字符移到新串的第一的位置,地址减一,字串长度计数器减一,判断计数器是否为0,不为0,重复移动字符,为零退出循环
然后按顺序显示字符串的方式显示
mips汇编的问题,divu s,t也可以用divu $zero, s, t替代?
可以,divu是R型指令,指令中用到三个寄存器,但除法的目标寄存器是hi 和 lo,所以rd位是无效的。divu $s2,$s0, $s1这样的指令也是对的。。。。
转载请注明出处51数据库 » mips汇编软件 MIPS汇编程序