0
.pos 0x200
.align 4
InputArray: 
 .long 5
 .long 10
Done: .long 0x0

.pos 0x400
.align 4
OutputArray:

.pos 0x0
 irmovl InputArray,%eax
 irmovl OutputArray, %esi

Loop:  
 mrmovl (%eax), %ecx # get first element from InputArray
 mrmovl (%eax), %edi # a copy of first element used for multiplication
 irmovl $4, %ebx # increment the pointer of InputArray
 addl %ebx, %eax
 mrmovl (%eax), %edx # get second element from InputArray
 irmovl $1, %ebx # add first element to its copy for the amount of second element
 subl %ebx, %edx
 jg mult
 rmmovl %ecx,(%esi) # output value to OutputArray

mult:
 addl %edi, %ecx
 subl %ebx, %edx
 jg mult

Exit: halt

这是一个非常简单的程序,可以对我为 Y86 编写的一对整数(5 乘 10)进行无符号乘法运算。

当我运行代码时,结果如下所示:

Stopped in 38 steps at PC = 0x42.  Status 'HLT', CC Z=1 S=0 O=0
Changes to registers:
%eax:   0x00000000      0x00000204
%ecx:   0x00000000      0x00000032
%ebx:   0x00000000      0x00000001
%esi:   0x00000000      0x00000400
%edi:   0x00000000      0x00000005

Changes to memory:

计算结果的寄存器 ecx 是十六进制的 32,所以我肯定知道 mult 循环已按预期执行,但没有任何内容输出到 OutputArray,我不明白为什么。

4

1 回答 1

0

我不确定我是否理解“标签中的跳转语句之后不应有任何代码,正确的替代方法是在另一个标签中找到后续代码并使用另一个跳转语句来调用该标签”的意思,但是您的代码的问题是您在 Mult 循环中忘记了 ret 语句。

此外,一旦您完成了代码的循环部分,您的代码就会不正常地终止(在遇到停止之前,您再次运行了 Mult 循环代码。)

以下代码有效(但请注意,它并不理想——除其他外,它不考虑负值和溢出):

.pos 0x200
.align 4
InputArray: 
 .long 5
 .long 10
Done: .long 0x0

.pos 0x400
.align 4
OutputArray:

.pos 0x0
 irmovl InputArray,%eax
 irmovl OutputArray, %esi

Loop:  
 mrmovl (%eax), %ecx # get first element from InputArray
 mrmovl (%eax), %edi # a copy of first element used for multiplication
 irmovl $4, %ebx # increment the pointer of InputArray
 addl %ebx, %eax
 mrmovl (%eax), %edx # get second element from InputArray
 irmovl $1, %ebx # add first element to its copy for the amount of second element
 subl %ebx, %edx
 jg mult
 rmmovl %ecx,(%esi) # output value to OutputArray
 jmp Exit

mult:
 addl %edi, %ecx
 subl %ebx, %edx
 jg mult
 ret

Exit: halt
于 2015-11-18T01:36:55.967 回答