1

I need simply to grab the first character from a string and keep its decimal value (according to ASCII: if I grab 'h' - I want to keep 104).

I was trying to find this and tried everything - nothing works for me.

I'm getting my pointer to the first char in string in rdi. Result in rax. I tried this

movb (%rdi), %al
ret

al - byte version of rax. As I know, 8 bits are used to store char.

If in my main.c I first put result in the character and then output as a decimal it perfectly works. But if I right away output result of assembly code as an integer it gives me garbage. If I check this assembly code with gdb, using i r al before the output to see the value of al it gives me right answer.

More than that, if I run program multiple times, it always give me different 'garbage'.

How to fix it?

Thank you.

Michael
  • 57,169
  • 9
  • 80
  • 125
beervoley
  • 11
  • 1
  • 5
  • 6
    Please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Oct 06 '16 at 08:59
  • 2
    I guess you're printing a string from the address of your output variable. You have to transfer it to a temporary array and add terminator at the end. – acegs Oct 06 '16 at 09:02
  • We have no idea what _"output result of assembly code as an integer"_ means here since you haven't shown us that part. – Michael Oct 06 '16 at 10:07
  • Did you write the prototype as `int`? Because that implies that the character value is sign extended to fill EAX, but you left garbage in the rest of RAX by only modifying AL. – Peter Cordes Oct 06 '16 at 17:42
  • @PeterCordes, that worked, thank you. Just had to clean RAX in the beginning. – beervoley Oct 07 '16 at 05:57
  • Or better, use a version of MOVZX (aka in AT&T syntax, `movzbl (%rdi), %eax`) – Peter Cordes Oct 07 '16 at 16:00

0 Answers0