nasm 将两个实数输入到2个不同的数组中
我正在尝试将实数输入到 2 个不同的数组中
,目前我有此代码,但在我将最后一个浮点值输入到 array2 后,它立即出现段错误,
有什么想法吗?
segment .bss
;
segment .data
prompt1 db "Do you have data to enter? (-1 = yes,0 = no)?: ", 0
prompt2 db "Enter your Float Value: ", 0
prompt3 db "Almost done ",0
segment .text
extern readdouble,print_string, read_int
global readarray
readarray:
pusha
mov ebx, [esp+36] ;move starting position into ebx
mov esi, [esp+40] ;move max values into edx
mov edi, 0 ;initialize counter to zero
read_loop:
mov eax, prompt1
call print_string
call read_int ;read in decision for prompt
inc edi; increment counter
cmp eax, 0
jz Done_reading_array1
jmp continue_loop
continue_loop:
mov eax, prompt2
call print_string
call readdouble
mov [ebx], ecx ;move value into memory slot ebx
mov [ebx+4], edx
add ebx, 8 ;move to next location for db word
jmp read_loop
Done_reading_array1:
sub edi, 1
mov [esp+40], edi ;moves counter back to stack
jmp read_array2
read_array2:
mov ebx, [esp+68] ;move starting location of array1 into ebx
;mov esi,[esp+80] ;move number of items into esi
mov ebp, 0
continue_readarray2:
mov eax, prompt2
call print_string
call readdouble
mov [ebx], ecx ;move value into memory slot ebx
mov [ebx+4], edx
inc ebp
add ebx, 8 ;move to next location for db word
cmp ebp, edi
jz done_reading_array2
jmp continue_readarray2
done_reading_array2:
;mov [esp+72],edi
mov eax, prompt3
call print_string
popa
ret
I'm trying to enter real numbers into 2 different arrays
currently I have this code, but it seg faults right after i'm done entering the last float value into array2,
any ideas?
segment .bss
;
segment .data
prompt1 db "Do you have data to enter? (-1 = yes,0 = no)?: ", 0
prompt2 db "Enter your Float Value: ", 0
prompt3 db "Almost done ",0
segment .text
extern readdouble,print_string, read_int
global readarray
readarray:
pusha
mov ebx, [esp+36] ;move starting position into ebx
mov esi, [esp+40] ;move max values into edx
mov edi, 0 ;initialize counter to zero
read_loop:
mov eax, prompt1
call print_string
call read_int ;read in decision for prompt
inc edi; increment counter
cmp eax, 0
jz Done_reading_array1
jmp continue_loop
continue_loop:
mov eax, prompt2
call print_string
call readdouble
mov [ebx], ecx ;move value into memory slot ebx
mov [ebx+4], edx
add ebx, 8 ;move to next location for db word
jmp read_loop
Done_reading_array1:
sub edi, 1
mov [esp+40], edi ;moves counter back to stack
jmp read_array2
read_array2:
mov ebx, [esp+68] ;move starting location of array1 into ebx
;mov esi,[esp+80] ;move number of items into esi
mov ebp, 0
continue_readarray2:
mov eax, prompt2
call print_string
call readdouble
mov [ebx], ecx ;move value into memory slot ebx
mov [ebx+4], edx
inc ebp
add ebx, 8 ;move to next location for db word
cmp ebp, edi
jz done_reading_array2
jmp continue_readarray2
done_reading_array2:
;mov [esp+72],edi
mov eax, prompt3
call print_string
popa
ret
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你不应该在第二个循环中使用 EBP,因为你搞砸了堆栈框架 - 为什么不使用 ESI 作为计数器呢。
I don't think you should be using EBP like that in the second loop as you're screwing up the stack frame - why not use ESI for the counter instead.
我没有看到您对 readarray 中分配的数组的长度进行任何边界检查。例如(也许是因为你的标签在这里搞砸了),但你似乎将 EBX 设置为数组的起始值,并将 ESI 设置为长度那个数组。但是在
read_loop
和continue_loop
内部,我无法检测到您实际上正在检查ESI
的值,看看是否可以继续增加对于从readdouble
返回的四字,EBX
乘以 8 个字节。在增加 EBX 之前,在循环中的某个位置,您必须检查 EBX 指向的已分配内存数组的可用剩余长度,这是一个值我假设您可以根据传递到 ESI 的信息进行构造(希望您调用的任何函数都不会破坏 ESI,否则您将不得不保存该值到堆栈或另一个被调用者保存寄存器中)。希望这有帮助,
杰森
I don't see you doing any bounds-checking on the length of the allocated array in
readarray
. For instance (and maybe it's because your labeling is messed up here), but you seem to setEBX
to the starting value of the array, and setESI
to the length of that array. But nowhere inside ofread_loop
andcontinue_loop
can I detect you're actually checking the value ofESI
to see if you can keep incrementing the value ofEBX
by 8-bytes for the quad-words being returned fromreaddouble
. Somewhere in that loop before you incrementEBX
you're going to have to check against the available remaining length of the allocated memory array being pointed to byEBX
, which is a value I'm assuming you can construct from the information passed intoESI
(and hopefully none of the functions you're calling destroyESI
, otherwise you're going to have to save that value to the stack or into another callee-save register).Hope this helps,
Jason