views:

360

answers:

1

I wanna create a substring (ministring) of 3 asciz chars out of my original (thestring). The thing ain't printing when being run so I don't know what the hell I'm I doing. Why it ain't printing? Am I creating the ministring correctly?

.section .data

thestring: .asciz "111010101"

ministring: .asciz ""

formatd:    .asciz "%d"
formats:    .asciz "%s"
formatc:    .asciz "%c"




.section .text

.globl _start

_start:

xorl %ecx, %ecx

ciclo:movb thestring(%ecx,1), %al
movzbl %al, %eax
movl %eax, ministring(%ecx,1)
incl %ecx
cmpl $3, %ecx
jl ciclo


movl thestring, %eax
pushl %eax
pushl $formats
call printf
addl $4, %esp


movl $1, %eax
movl $0, %ebx
int $0x80
+1  A: 

You haven't reserved enough memory space to contain the null-terminated ministring which you're creating ... therefore, when you write to this memory, you're overwriting the value of formatd and formats (and so you're eventually passing something other than "%s" to printf).

Instead of your definition of the ministring memory location, try using the following :

ministring: .asciz "   "


Also, instead of this:

movl %eax, ministring(%ecx,1)

I don't understand why you aren't using this instead:

movb %al, ministring(%ecx,1)


Also, if you want to print the ministring, then instead of this:

movl thestring, %eax

Do this:

movl ministring, %eax


Also instead of this:

addl $4, %esp

Why not this:

addl $8, %esp


ALso I suggest that you use a debugger to:

  • Step through the code
  • Watch the values contained in registers and in memory as you step through
  • Know the location of any segmentation fault
ChrisW
Segmentation fault
omgzor
I edited my post, to add a suggestion to use movb instead of movl.Also, perhaps you ought to use be using debugger to step through this code, and to examine memory and register contents.
ChrisW
Did both changes. Still gives Segmentation Fault
omgzor
I'll edit again to also suggest that you swap the sequence in which you push parameters.
ChrisW
No I won't suggest that (but, "addl $4, %esp" would make more sense to me than "addl $4, %esp").
ChrisW
I meant, "addl $8, %esp" would make more sense to me.
ChrisW
Yeah, but that last instruction is inconsequential if the system and exit calls are next. You can even omit the addl if you won't be working with the stack anymore. Did all changes suggested and ended up with Segmentation Fault still.
omgzor
You keep saying that and I keep having to guess: so, *where* is it faulting?? Use a debugger!! Also (I'm not familiar with this assembler) is the "pushl $formats" statement pushing the address, or pushing the content, of the 'formats' memory? Would "pushl formats" be any better?
ChrisW