程序集 (x86) 循环分段错误 [英] Assembly (x86) loop segmentation fault

查看:26
本文介绍了程序集 (x86) 循环分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经声明了两个空格,我将把它们用作一个数组.(无论如何,这就是我所希望的)

I've declared two spaces which i am going to use as an array. (That is what i am hoping for anyway)

WORM_X: .space 128
WORM_Y: .space 128

它们将保存 X 和 Y 坐标.

They will hold X and Y coordinates.

我试图将 som 值放入数组中,然后使用 nib_put_scr 将它们打印在屏幕上,这是一个使用 curses.h 的 C 函数.

I am trying to put som values in the arrays, and then print them on on the screen using nib_put_scr thats a C-function that's using curses.h.

当我运行此代码时,出现分段错误.现在有人做错了吗?

When i run this code i get segmentation fault. do anyone now what i am doing wrong?

(顺便说一句,我是一个完整的汇编初学者)

(I'm a complete beginner on assembly btw)

# Sets up the WORM_Y array
    mov LENGTH, %eax
    add Y, %eax     
    mov %eax, CMP
    mov $WORM_Y, %eax
    mov Y, %ebx

loop_addy:

    mov %ebx, 0(%eax)
    add $4, %eax
    inc %ebx
    cmp CMP, %ebx
    jne loop_addy

# Sets up the WORM_X array
mov LENGTH, %eax
    add X, %eax     
    mov %eax, CMP
    mov $WORM_X, %eax
    mov X, %ebx
    mov X, %ecx

loop_addx:

    mov %ecx, 0(%eax)
    add $4, %eax
    cmp CMP, %ebx
    jne loop_addx


# Prints out signs on the screen with coordinates WORM_X & WORM_Y
    mov $WORM_X, %ebx
    mov $WORM_Y, %edx

loop_printtest: 

    push    $48
    push    (%ebx)
    push    (%edx)
    call    nib_put_scr
    addl    $12, %esp

    add $4, %ebx
    add $4, %edx

    mov (%ebx), %ecx
    cmp $0, %ecx
    jne loop_printtest

推荐答案

调用库函数通常会销毁 eax/ecx/edx 寄存器.我猜对 nib_put_scr 的调用正在破坏这些寄存器的内容,位于 ncurses 内部.

Calling a library function will normally destroy the eax/ecx/edx registers. I'm guessing that the call to nib_put_scr is destroying the contents of these registers somewhere further down the line, inside ncurses.

您可以通过使用操作码包装函数调用来存储/恢复所有寄存器来轻松测试这一点:

You can easily test this by wrapping the function call with opcodes to store/restore all registers:

pushal  ;  store all regs

; Call function as usual
push    $48
push    (%ebx)
push    (%edx)
call    nib_put_scr
addl    $12, %esp

popal   ; restore all regs

如果这有帮助,那么您已经找到了问题所在.然后,您只需要确保在函数调用期间保留寄存器.这通常是通过在函数调用之前将寄存器值推入堆栈,然后在之后将它们的值弹出来完成的.

If that helps then you've found the problem. You then only need to make sure that registers are preserved during function calls. This is usually done by pushing register values to the stack before a function call, and then popping their values back afterwards.

这篇关于程序集 (x86) 循环分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆