Reversing a string and printing it in IA32 assembly -


so i'm trying read string, increase counter (in case, %edx) until read \n, , then, going backwards print reversed.

this code:

.section .data cadsal:      .asciz "por favor ingrese su cadena:\n"  leer:     .asciz "%s"  salidafinal:     .asciz "la cadena introducida, invertida es:\n"  imp:     .asciz "%c\n" 

.section .bss .comm cadena,50,1

.section .text

.globl _start

_start:

leal cadsal, %eax pushl %eax call printf addl $4, %esp  leal cadena, %eax pushl %eax leal leer, %eax pushl %eax   call scanf addl $8, %esp  xorl %edx, %edx 

contar:

movb cadena(%edx), %al incl %edx cmpb $0, %al jne contar  leal salidafinal, %ecx pushl %ecx call printf addl $4, %esp  addl $-2, %edx 

invertir:

movb cadena(%edx), %al pushl %eax leal imp, %ebx pushl %ebx call printf addl $8, %esp  decl %edx cmpl $0, %edx jge invertir  movl $1, %eax int $0x80 

i'm using command -nostartfiles when compiling. works, when run it, segmentation fault appears when entering in "invertir". need detect what's wrong in code.

thanks!

your immediate problem cmpb '\n', %al missing $ sign required in at&t syntax make immediate. is, '\n' considered address, , invalid 1 @ that. should use cmpb $'\n', %al.

however scanf not place '\n' buffer @ all, because %s stops @ whitespace. want looking terminating 0 byte instead, use cmpb $0, %al. or, if need read whole line, use fgets instead.

if fix that, run next problem have incl %edx between cmp , jne change flags not branching on result of comparison increment. solution: swap cmp , incl.

yet problem registers, including %edx, caller-saved. such can modified called functions, , call printf. should push/pop around both of preserve value.

even after of that, won't see output until print final \n manually, due buffering.

note if intend use c library, should not use -nostartfiles. instead, should use entry point main , assemble , link normally. also, should return main (or @ call exit) , not use direct system call because doesn't allow c library clean properly. incidentally, flush i/o buffers, see output without newline.

you should learn use debugger.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -