2

https://www.reddit.com/r/Assembly_language/comments/wfse2i/trying_to_convert_an_ascii_to_int/

Here is the part of my code I am struggling with. The algorithm I'm supposed to use is:

Suppose ebx points to the (ASCII) number you just read in.

Zero %eax.

Loop until %cl contains 10 decimal (0a hex).

Move a byte from (%ebx) to %cl.

Subtract 48 decimal (or 30 hex) from %cl. This converts ASCII to an actual number. Multiply %eax by 10 and add %cl.This will build the int.

I'm struggling with the last bit where I'm supposed to multiply and add. From what i've learned to multiply you just put the number you want to multiply it will automatically multiply it by ax or in my case eax, but that doesn't work and I also don't understand How I'm supposed to add a 16 bit register to a 32 bit register. Any ideas? Thanks in advance.

string_end:
. equ
len, string_end - string
.text
.globl
start
start:
movl $WRITE, %eax
movl $STDOUT, %ebx
movl $string, %ecx
movl $len,%edx
int $0x80

movl $READ, %eax
movl $STDIN, %ebx
movl %esp, %ecx
movl $30, %edx

loop:
xor %eax, %eax
incb %cl
cmpb $10, %cl
je done
movb (%ebx), %cl
sub $48, %cl
mull $10
add %cl, %eax

done:
nop
geraldo_42
  • 21
  • 2
  • If you cross post from reddit, please put a link to the other question here so no effort is duplicated. – fuz Aug 04 '22 at 05:32
  • @fuz fixed it, srry. – geraldo_42 Aug 04 '22 at 05:34
  • You're writing 32-bit code; you don't have to use EAX to multiply, and shouldn't. Use `imul $10, %eax` (or any other register) to multiply it by 10. https://www.felixcloutier.com/x86/imul. You zero-extend your character from 8-bit to 32-bit so you have a 32-bit itneger to add. – Peter Cordes Aug 04 '22 at 08:50
  • Also, `. equ` looks like a serious syntax error introduced by copy/paste. Also having the intended operands on the next line, `len, string_end - string` – Peter Cordes Aug 04 '22 at 08:51

0 Answers0