0

Current program takes in command line args and prints them to screen, compiled with gcc. The stack contains addresses that point to the arguments taken in. I need to convert the two arguments, add them and print the result.

I've tried adding the value by dereferencing the registers like this:

    movl (%edi), %edi
    addl %edi, (%esi)

I've also tried subtracting "0" to convert the string

   sub "0", (%esi) 

My partial code is:

printLoop:
    movl    12(%ebp), %esi          # Get **argv pointer to the vector table
    movl    (%esi,%ebx,4), %esi     # Use the pointer to indirectly load the address of the
                                    # next command line argument. %ebx is the index
    incl    %ebx    
    movl    12(%ebp), %edi                       
    movl (%edi, %ebx, 4), %edi      #pointer to next arg in edi?
    
    #sub "0", (%esi) 
    
    movl (%edi), %edi    # don't work
    addl %edi, (%esi)    # don't work
      

    call    printString             # print the null terminated command line argument

    movl    $newLine, %esi          # go to the next line
    call    printString             # prints the arg

I've also tried saving the value of the second arg in %edx and then adding, it doesn't work.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • [Convert from ascii to integer in AT&T Assembly](https://stackoverflow.com/q/32034178) for input, [Printing an integer as a string with AT&T syntax, with Linux system calls instead of printf](https://stackoverflow.com/a/45851398) for output (that's 64-bit code, but the algorithm is the same.) [Add 2 numbers and print the result using Assembly x86](https://stackoverflow.com/a/28524951) for 32-bit NASM. – Peter Cordes Mar 11 '23 at 01:46
  • [What's difference between number with $ or without $ symbol in at&t assembly syntax?](https://stackoverflow.com/q/18996870) re: `sub "0", (%esi)` which tries to use an absolute memory operand instead of an immediate. – Peter Cordes Mar 11 '23 at 01:47
  • That code is printing strings. Why do you need ints? – Erik Eidt Mar 11 '23 at 14:05
  • @ErikEidt: Hopefully as a proof-of-concept, or building block for a program that does something with integers, and outputs a different integer than the one you input. Seems like a very reasonable thing to want to do, with the next step being to add `imul %eax,%eax` to print the square of a number from `argv[1]` or something. – Peter Cordes Mar 12 '23 at 03:59
  • @PeterCordes, seems folly to attempt to convert to int and still print string. Of course, we don't know what they really have tried. – Erik Eidt Mar 12 '23 at 04:19
  • @ErikEidt: Well yeah, obviously you have to convert back from int to string like they asked for in the title. The fact that their attempt is on the wrong track doesn't invalidate the title question. (Although it's maybe so far from working that it's not a good debugging question, and should maybe just get closed as a duplicate of the questions I linked in a comment.) – Peter Cordes Mar 12 '23 at 04:22

0 Answers0