0

I'm currently looking to get an input from the user. This input should be in a string form but should be composed of numbers, like 1234. I don't understand the whole ASCII conversion thing. I do have user input here but don't understand the conversion from string to int. This is what I have so far:

    msg: .ascii "Insert a number:"
    .equ STDOUT,1
    .equ WRITE,4
    .equ EXIT,1
    .equ STDIN,0
    .equ READ,0
msg_end:
    .equ len, msg_end - msg

.text
    .globl _start

_start:
        movl $WRITE, %eax   #write to eax
        movl $STDOUT, %ebx  #print ebx
        movl $msg, %ecx     #move string to ecx
        movl $len, %edx     #length of string to edx
        int $0x80
        
        movl $READ, %eax    #read
        movl $STDIN, %ebx       
        movl %esp, %ecx     #starting point
        movl $6, %edx       #max input we would have
        int $0x80
        
        movl $1, %ecx       #counter ecx
_input:                     #subroutine to check when at the end of user input
        xor %ebx, %ebx
        movb (%esp), %bl
        inc %esp            #move on to the next char
        inc %ecx            #counter moves on by 1
        cmp $0xa, %ebx      #compare with \n
        jne _input          #if not loop back up
        
        sub %ecx, %esp      #start from the first input char
        movl $WRITE, %eax   #write to eax
        movl $STDOUT, %ebx  #print out
        movl %ecx, %edx     #pointer to edx
        movl %esp, %ecx     #length of input
        int $0x80
        
        movl $EXIT, %eax
        int $0x80

Sorry for giving the entire code but I'm not sure exactly how to do this.

  • If you just want to print the number again, you don't actually need to covert it to a binary integer in a register and back, unless you're doing something with the number in between. BTW, the `read` system-call return value is the length in bytes, so you don't need to loop over the buffer. – Peter Cordes Aug 06 '21 at 06:14
  • I just want to get the number the user inputted as a string to be converted to a normal integer and be reprinted out again. – ASM student Aug 06 '21 at 06:24
  • Ok, then it's a duplicate of [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894) and [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) which show how to do each of those things separately. – Peter Cordes Aug 06 '21 at 06:48

0 Answers0