nasm 将两个实数输入到2个不同的数组中

发布于 2024-10-30 23:52:15 字数 1709 浏览 2 评论 0原文

我正在尝试将实数输入到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意犹 2024-11-06 23:52:15

我认为你不应该在第二个循环中使用 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.

梓梦 2024-11-06 23:52:15

我没有看到您对 readarray 中分配的数组的长度进行任何边界检查。例如(也许是因为你的标签在这里搞砸了),但你似乎将 EBX 设置为数组的起始值,并将 ESI 设置为长度那个数组。但是在 read_loopcontinue_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 set EBX to the starting value of the array, and set ESI to the length of that array. But nowhere inside of read_loop and continue_loop can I detect you're actually checking the value of ESI to see if you can keep incrementing the value of EBX by 8-bytes for the quad-words being returned from readdouble. Somewhere in that loop before you increment EBX you're going to have to check against the available remaining length of the allocated memory array being pointed to by EBX, which is a value I'm assuming you can construct from the information passed into ESI (and hopefully none of the functions you're calling destroy ESI, otherwise you're going to have to save that value to the stack or into another callee-save register).

Hope this helps,

Jason

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文