我正在尝试编写一个程序来创建 x 和 y 之间所有包含整数的和,其中 sum、y 和 x 是全局变量。当我尝试将 x 和 y 分配给本地寄存器时遇到问题(我的模拟器将 0x60 和 0x64 的值分配给本地寄存器,而不是 1 和 4)以及获取总和并将其传输到sum 的全局变量。通常我会尝试在网上找到有用的指南,但 Y86 是一种很少使用的语言,几乎没有。
我的代码:
.pos 0
init: irmovl Stack, %esp //Set up stack pointer
irmovl Stack, %ebp //Set up base pointer
call main //call main program
halt //Terminate program
main: pushl %ebp //setup
rrmovl %esp, %ebp
pushl %ebx //declaring x local register
irmovl x, %ebx
pushl %esi //declaring y local register
irmovl y, %esi
pushl %eax //declaring sum
irmovl sum, %eax
pushl %edi //constant of 1
irmovl $1, %edi
L2:
subl %ebx, %esi // %esi = y-x
jl L3 // ends function if x > y
irmovl y, %esi // %esi = y
addl %ebx, %eax // sum += x
addl %edi, %ebx // x++
jmp L2 // go to beginning of loop
rmmovl %eax, (%eax) //value assigned to sum global variable
L3:
rrmovl %ebp, %esp //finish
popl %ebx
popl %esi
popl %edi
popl %eax
popl %ebp
ret
.align 4
x: .long 1
y: .long 4
sum: .long 0
.pos 0x200
Stack: .long 0