简体   繁体   English

ori指令在此代码中的重要性是什么?

[英]What is the ori instruction's importance in this code?

# Add the first five integers 

.text 0x3000
.globl main
main:
 add   $8,$0,$0     # sum = 0
 add   $9,$0,$0     # for (i = 0; ...
loop:
 add   $8,$8,$9     # sum = sum + i;
 addi  $9,$9,1      # for (...; ...; i++
 slti  $10,$9,5     # for (...; i<5;
 bne   $10,$0,loop
end: 
 ori   $v0, $0, 10     # system call 10 for exit
 syscall               # we are out of here.

I am not quite sure what the ori instruction is doing in the end. 我不太确定ori指令到底在做什么。
Once the loop has ended it has ended, so why is it significant? 一旦循环结束,它就结束了,那为什么有意义呢?

In this case, ori simply functions as mov . 在这种情况下, ori只是充当mov
This is because ori rd, rs, imm is defined as rd = rs | imm 这是因为ori rd, rs, imm被定义为rd = rs | imm rd = rs | imm . rd = rs | imm
When rs is the zero register, this simply means rd = imm . rs为零寄存器时,这仅表示rd = imm

If the CPU's instruction set doesn't have an actual mov instruction, a helpful assembler would translate the pseudo-instruction mov rd, imm to ori rd, $0, imm . 如果CPU的指令集没有实际的mov指令,则有用的汇编程序会将伪指令mov rd, immori rd, $0, imm


As for the loop and the ending, the thing is that an assembly language has no notion of "reaching the end of the program and exiting". 至于循环和结尾,问题是汇编语言没有“到达程序结尾并退出”的概念。 Suppose your last coded instruction is located at address 0xDEADBEEF. 假设您的最后一条编码指令位于地址0xDEADBEEF。 After the CPU executes the instruction, it simply moves on to the next instruction address at 0xDEADBEF0. CPU执行完指令后,它仅移至下一个指令地址0xDEADBEF0。 The next instruction address merely keeps incrementing until something happens, like a trap or whatnot. 下一条指令地址仅保持递增,直到发生某种情况,例如陷阱或诸如此类。

So the conventional way to terminate a program is to make a system call. 因此,终止程序的常规方法是进行系统调用。 This transfers execution to the OS, kills the process, and never gives execution back to the process. 这会将执行转移到OS,杀死进程,并且永远不会将执行交还给进程。 To make a system call, you would put in appropriate values into registers - in this case, the value 10 is a command that the system will interpret as "exit process". 要进行系统调用,您需要将适当的值放入寄存器中-在这种情况下,值10是系统将解释为“退出过程”的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM