3

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

They will hold X and Y coordinates.

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
Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
Bewn
  • 575
  • 4
  • 9
  • 13

1 Answers1

2

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.

Martin
  • 37,119
  • 15
  • 73
  • 82
  • thanks for the answer! It worked just fine after i used pushal and popal! Thanks alot! :) – Bewn Nov 22 '11 at 11:16