你好,我是新来的装配,我正在努力得到一个两部分的程序来工作。对于此x86程序集,我使用Visual。
第一部分)我的第一个目标是数到13,在路上把每个值加起来。前任,0+1+2+3...+13=91.我想把总数全部储存起来。
第二部分,我要用2^0到2^6的幂数,x,0,1,2,4,8,32,64。我想,我正在这么做,但我并不是在储存每一种价值。我想将这些存储在连续的内存位置中。
到目前为止我有这个,
.586
.MODEL FLAT
.STACK 4096
.DATA
num1 BYTE 13 ;Initialize number to count to
totall BYTE 0 ;Total of all counted numbers
temp BYTE 0 ;Temp for loop adding
shiftme BYTE 1 ;Start of counting 2^0 so I can reach 2^6
.CODE
main PROC
;code here
increment: ;Increment label
inc temp ;Increment temp by 1
mov eax, temp
add totall, eax ;Add temp+totall and store in totall
cmp eax, num1 ;Compare for jump
jne increment ;Jump if not equal
;this part should store each value 1,2,4,8,32.. in consecutive memory locat
shiftallthethings: ;Shift label
shl shiftme, 1 ;Shifting bits to the left one
cmp shiftme, 64 ;Comparing for the jump
jne shiftallthethings ;Jump if not equal to
ret
main ENDP
END
能帮我理解的问题。
任何帮助都是非常感谢的,谢谢。
发布于 2012-03-26 12:54:27
首先,在回答你们的问题时:
如何在连续内存位置存储值?
在MASM32中,您可以直接执行像mov sum_addr, eax
这样的mov
(只要数据类型具有相同的大小),也可以传递数据指针。在我编写的示例中,指向total
的指针被传递给函数。然后,该函数将一个值写入该指针所指向的内存(即total
的内容)。不太清楚连续的内存位置是什么意思。如果你是指指针运算,我也可以给你举一个例子。
我正确地使用了跳转和标签指令吗?
是的,那似乎很好。我使用的另一种方法是匿名标签。这是适当的,当标签是琐碎和相当接近。这是个人喜好。如果你觉得标签名称更合适,也可以随意使用。
,我需要使用像eax这样的特定寄存器来执行这些问题吗?为什么?
MASM32遵循Win32 (stdcall)的调用约定,所以这样做对您也有好处。在寄存器保存方面,这意味着除了eax
、ecx
和edx
之外,所有函数都需要保存寄存器,这些功能被认为是可传输的。如果需要超过4个字节,则返回值存储在eax
或eax
和edx
中。
就您编写的代码而言,您遇到了一些问题,例如试图将不同大小的数据类型相互移动。例如,如果将byte
移动到dword
中,则必须首先将其扩展为相同的大小。
mov eax, temp
这将不会编译,因为temp
只有1字节长,而eax
只有4字节。你可以做的是:
movzx eax, temp
在移动之前,这个零扩展的temp
。下面是一些我拼凑在一起的代码,可能会教你一些东西。它使用宏(不确定您是否还想学习这些宏),但其他方面演示了传递和返回值的惯用MASM32参数。
include \masm32\include\masm32rt.inc
.DATA
total DWORD 0 ;Total of all counted numbers
.CODE
; takes in address of total and writes the value there
sum PROC sum_addr:DWORD
xor eax, eax
xor ecx, ecx
@@:
inc ecx
add eax, ecx
cmp ecx, 13
jnz @b
mov edx, sum_addr
mov dword ptr ds:[edx], eax
print ustr$(eax), 13, 10
mov edx, sum_addr
mov eax, dword ptr ds:[edx]
ret
sum ENDP
start:
push offset total ; pass address of total
call sum
print ustr$(eax), 13, 10 ; demonstrating how to use return values
print ustr$(total), 13, 10 ; testing result got stored properly
ret
end start
END
代码没有被优化,但是应该很容易理解。注意我是如何尽可能多地使用寄存器的(如果我们有足够的寄存器,这比经常处理内存更有效)。
https://stackoverflow.com/questions/9878241
复制