第一个MIPS汇编
MIPS是什么?想必看到标题的第一时间,读者就会冒出这个问题。
MIPS是一种RISC指令集,是一种无流水线互锁处理器(Microprocessor without Interlocking Pipeline Stage)的架构,是相对于Inter 8086的x86汇编而言的一种指令集,它们有一些不同之处,主要体现在寄存器的使用,以及寻址方式,指令格式上。这些问题今天先不讨论。
今天我想要介绍的是,如何在一个MIPS汇编模拟器上进行第一个MIPS汇编程序的编写即hello.s程序的编写。
MIPS汇编模拟器有很多种,最常用的是SPIM(贴一个网址吧,SPIM MIPS Simulator (wisc.edu))
但是我用的还是在实验的时候老师推荐我们使用的一款模拟器WinMIPS64
一开始使用它的时候,我还不太清楚如何编写MIPS汇编程序,看了WinMIPS64软件中的教程,我才知道如何编写我的第一个hello程序。
第一个MIPS程序
1 .data 2 hello: .asciiz 'hello\n' 3 CR: .word32 0x10000 4 DR: .word32 0x10008 5 6 .text 7 8 9 lwu r1, CR(r0) ;control register 10 lwu r2, DR(r0) ;data register 11 daddi r3, r0, hello 12 daddi r10, r0, 4 13 sd r3, (r2) ;output r3 14 sd r10, (r1) ; output to screen 15 16 halt
解释
CR代表的是控制寄存器control register的端口号
DR代表的是数据寄存器data register 的端口号
daddi r3, r0, hello daddi r10, r0, 4
前面一条指令,让数据寄存器端口得到了hello字符串的地址
后面一条指令,让控制寄存器端口得到了4
这个4是什么意思?想必读者会产生疑惑
这时我们打开软件随带的教程文件winmips64 tutorial.pdf
查看其中关于IO操作的说明
WinMIPS64手册--IO操作说明
Memory Mapped I/O area
Addresses of CONTROL and DATA registersCONTROL: .word32 0x10000DATA: .word32 0x10008Set CONTROL = 1, Set DATA to Unsigned Integer to be outputSet CONTROL = 2, Set DATA to Signed Integer to be outputSet CONTROL = 3, Set DATA to Floating Point to be outputSet CONTROL = 4, Set DATA to address of string to be outputSet CONTROL = 5, Set DATA+5 to x coordinate, DATA+4 to y coordinate,and DATA to RGB colour to be outputSet CONTROL = 6, Clears the terminal screenSet CONTROL = 7, Clears the graphics screenSet CONTROL = 8, read the DATA (either an integer or a floatingpoint) from the keyboardSet CONTROL = 9, read one byte from DATA, no character echo.
我标出的部分,可以解释为什么将控制寄存器端口内容置为4。
PS:
有x86汇编经历的同学应该发现了,这里有点类似于x86汇编里面的
mov dx,offset hello
mov ah,09h
int 21h
就是一个系统功能调用的功能号
附上WInMIPS64的手册内容WinMIPS64 — Documentation Summary (dcu.ie)
对x86汇编记忆有些模糊了的小伙伴也可以看一下x86汇编知识Guide to x86 Assembly (virginia.edu)
结语
由于不知道如何使用图片上传功能,所以就没办法把截图放在这里啦,谢谢读者的耐心阅读,博主写作经验不足,烦请斧正。如有各种问题欢迎留言交流^_^