0

I'm writing an assembly program with AT&T syntax (GAS compiling) on an Intel processor and on Ubuntu 14.

I'm trying to read the number of parameters passed by the user at the moment of program launch. For instance if user open his terminal and types ./programname x y z I'd like that the nParameters variable assumes value 4 (because programname, x, y and z are parameter).

This is what I've done so far but I keep getting a segementation fault error.

.code32
.section .data

nParameters: .byte 0

.section .text
    .global _start

_start:
    popl %eax   #extract the number of parameters on the stack
    movl %eax,nParameters

movl $1,%eax
movl $0,%ebx
int $0x80

The error lies on this line: movl %eax,nParameters but I don't know why

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
splunk
  • 6,435
  • 17
  • 58
  • 105
  • 1
    `movl %eax,nParameters` will try to move the contents of EAX (32-bit) to nParameters which is defined by an 8-bit byte. Maybe you intended to make it `.long`. But I have a suspicion you might be creating a 64-bit executable with 32-bit code? Can you show us the commands you use to assemble and link to an executable? – Michael Petch Jun 15 '16 at 08:10
  • @MichaelPetch as -o programname.o programname.s and I link this way: ld -o test programname programname.o. I replaced .byte 0 with .long but still get the same error. Yes, I'm on a 64-bit system – splunk Jun 15 '16 at 08:28
  • 1
    I will make a guess you are on 64-bit linux? If you are, try assembling and linking with `as --32 -o programname.o programname.s` and `ld -melf_i386 -o programname programname.o` . That should generate a 32-bit executable. – Michael Petch Jun 15 '16 at 08:31
  • @MichaelPetch solved it. I assembled in this way: as --32 -gstabs and linked with ld -melf_i386. thank you for your time – splunk Jun 15 '16 at 08:58
  • The [x86 tag wiki](http://stackoverflow.com/tags/x86/info) has an entry on building 32bit code on a 64bit system. It's in the FAQ section. You'll also find links to the ABI docs. – Peter Cordes Jun 15 '16 at 09:30
  • Possible duplicate of [Assembling 32-bit binaries on a 64-bit system (GNU toolchain)](http://stackoverflow.com/questions/36861903/assembling-32-bit-binaries-on-a-64-bit-system-gnu-toolchain) – Peter Cordes Jun 15 '16 at 09:33
  • 1
    You needing to put `.code32` in front is a dead giveaway that you are assembling this as a 64 bit program :-) – fuz Jun 15 '16 at 12:13
  • 1
    The requirement for `-gstabs` is perplexing. I see no reason why it is necessary. Looking back you have this in your comment `ld -o test programname programname.o` . `-o test` says what the outputfile will be so I don't know what the extra `programname` is right after. – Michael Petch Jun 15 '16 at 14:01

0 Answers0