flat assembler
Message board for the users of flat assembler.

flat assembler > Linux > Segmentation Fault ERROR

Author
Thread Post new topic Reply to topic
bluechicken



Joined: 27 Jun 2005
Posts: 1
Location: n/a
why do i get a SEGMENTATION FAULT error when i run the follow C program with the assembly code.



================assembly====================
regstack = 4 * 8 # There are 8 32-bit registers on the stack
arg1 = regstack + 8 # The offset for arg1 is 4 greater to skip ret add
arg2 = arg1 + 8
arg3 = arg2 + 8
.globl add4 #function name( yours is add4)m
add4: pushal # save all the registers
movl %esp, %ebp # and set the frame pointer
movl arg1(%ebp), %esi# esi points to the source number
movl arg2(%ebp), %edi# and edi the destination
movl arg3(%ebp), %ecx# ecx gets the longword count
clc # start with c-bit = 0
l1: movl (%esi), %eax # get next longword of source
adcl %eax, (%edi) # and add it to the destination
leal 4(%esi), %esi # bump both pointers
leal 4(%edi), %edi
decl %ecx # decrement the loop counter
# (note this does not affect the c-bit)
jnz l1 # loop until 0
movl $0, regstack-4(%ebp) # clear the location of %eax
adcl $0, regstack-4(%ebp) # and add in the c-bit
popal # then restore all the registers
ret # and return



=============c program=============
#include <stdio.h>
int add4(unsigned int a[], unsigned int b[], unsigned int c[]);
int main()
{
unsigned int a[4], b[4], c[4], carry, rem;
while(1)
{
printf("a=");
scanf("%x%x%x%x", &a[3], &a[2], &a[1], &a[0]);
printf("b=");
scanf("%x%x%x%x", &b[3], &b[2], &b[1], &b[0]);
carry = add4(a, b, c);
printf("add %d%9.8X%9.8X%9.8X%9.8X\n", carry, c[3], c[2], c[1], c[0]);
if(!(carry||c[0]||c[1]||c[2]||c[3]))
return 0;
}
}

_________________
n/a
Post 27 Jun 2005, 16:00
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger Reply with quote
Endre



Joined: 29 Dec 2003
Posts: 212
Location: Budapest, Hungary
I made some corrections on your program and it works now.

Code:
arg1 = 8 # The offset for arg1 is 8 to skip ret address and %ebp on the stack
arg2 = arg1 + 4 
arg3 = arg2 + 4 

.globl add4 #function name( yours is add4)m 
add4:
        pushl   %ebp
        movl    %esp, %ebp # and set the frame pointer 
        pushl   %ebx
        pushl   %edi
        pushl   %esi
        movl    arg1(%ebp), %eax
        movl    arg2(%ebp), %ebx
        movl    arg3(%ebp), %ecx
        movl    $3, %edx# edx gets the longword count 
        xorl    %edi, %edi
l1:
        addl    %edi, %edi
        pushl   %edi
        movl    (%eax, %edx, 4), %edi
        addl    (%ebx, %edx, 4), %edi
        movl    %edi, (%ecx, %edx, 4)
        popl    %edi
        jnc     skip
        orl     $1, %edi
skip:
        decl    %edx
        jns     l1
        movl    %edi, %eax
        popl    %esi
        popl    %edi
        popl    %ebx
        popl    %ebp
        ret     # and return     
Post 29 Jun 2005, 09:52
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.