0

basically the title, task is that we need to turn a string number into an int number through assembly, I think I found a code that might be working, but there is one error I can't seem to be able to solve, it's an error "operand size conflict" in a line that's:

add eax, temp_char

#include <stdio.h>
#include <string.h>

int main(int argc, char** argv) {

    int iLettersCount;
    char* argv1 = argv[1];

    if (argc < 2) {
        printf("Nepateiktas parametras\n");
        return(0);
    }


    __asm {
        atoi :
        mov eax, 0
        mov ecx, argv1

            convert:
            mov al, byte ptr [ecx]
            mov temp_char, al
            cmp temp_char, '0'
            je done

            cmp temp_char, 48
            jl error

            cmp temp_char, 57
            jg error

            sub temp_char, 48
            imul eax, 10
            add eax, temp_char

            inc rdi
            jmp convert

            error :
            mov eax, -1

            done :
            ret 


};


    return(0);
}

Thank you for any suggestions and answers!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Lofu Fox
  • 3
  • 2
  • 3
    What do you need `temp_char` for anyway? Just use a register. Note that `al` is part of `eax`, so you're currently overwriting parts of your running result when you do `mov al, byte ptr [ecx]`. – Michael May 04 '21 at 17:48
  • Also, I suspect that `cmp temp_char, '0'` is supposed to be `cmp temp_char, '\0'` (or simply `cmp temp_char, 0`). – Michael May 04 '21 at 17:51
  • The hardware doesn't define an 8-bit to 32-bit addition operation, so you'll have to find another way; for example, upgrade the 8-bit value to equivalent number in 32-bits and do 32-bit arithmetic. – Erik Eidt May 04 '21 at 17:55
  • For a working efficient version, see [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) – Peter Cordes May 04 '21 at 18:00
  • FYI, register layout, `al` and `ah` (8-bits) are part of `ax`(16-bits), which is part of `eax` (32-bits). – Thomas Matthews May 04 '21 at 18:01

0 Answers0