-1

How do I convert a string of number (decimals) to a integer (binary) using IA-32 assembler in c++?

Here is a shell of what I need.

#include <stdio.h>
int main(int argc, char** argv) {

int iOut = 0;
char* pcInp;

if (argc < 2) {
    printf("Mssing parameter: number\n");
    return(0);

}

pcInp = argv[1];

_asm {

    push aex
    push ebx
    push ecx
    push edx

    //code here

    pop edx
    pop ecx
    pop ebx
    pop eax


}

printf("Number was processed as %d\n", iOut);
}
Mathieu
  • 8,840
  • 7
  • 32
  • 45
SurisDziugas
  • 160
  • 1
  • 3
  • 12
  • What's this got to do with C++? – George Dec 05 '16 at 10:14
  • @george I meant C, mixed up. – SurisDziugas Dec 05 '16 at 10:17
  • I do know basics of c and cpp, tho I don't know assembly at all, and I need this to be in assembly. Id have no problem doing this in cpp. – SurisDziugas Dec 05 '16 at 10:26
  • 1
    Good, That's like 80% of the work! Do it in C and update the answer. Of course using functions like `atoi` is not useful. We can help translate the C code into assembly as long as you are a specific problem. – Margaret Bloom Dec 05 '16 at 10:40
  • Im stuck w/o atoi. – SurisDziugas Dec 05 '16 at 10:59
  • Write it in C (without `atoi`) and look at the compiler's assembly output (`gcc -S`). Why do you need it in assembly ? It it homework ? – Jabberwocky Dec 05 '16 at 11:01
  • Yes, its homework. I normally would use c++ and I have problems with c and assembly. – SurisDziugas Dec 05 '16 at 11:03
  • 2
    all you need is something like `foreach digit in string { value = 10*value + value_of(digit); }` , and the value of a digit is it's ascii-value - '0' – Tommylee2k Dec 05 '16 at 11:22
  • So learn how `atoi` works internally (I mean the principle). You actually don't have to read anything programming related, just some math course how to convert decimal<->binary numbers on paper may enlighten you enough, what to do with particular string character bytes. If you are still desperate, I just answered somebody with same problem, but in MIPS: http://stackoverflow.com/a/40972859/4271923 (the answer is general enough to give you clues for x86 as well). (but you actually want decimal string to value, not to binary, so Tommylee2k advice is close to solution) – Ped7g Dec 05 '16 at 12:31
  • 1
    If you Google for "atoi asm", you get hundreds of useful hits. You should put some actual effort into developing a solution of your own first, and then ask a *specific* question when you run into a problem or something that you're confused about. The way this question reads now is basically "write my code for me". The "// code here" comment is particularly egregious, like *we're* the ones writing an exam. – Cody Gray - on strike Dec 05 '16 at 12:42
  • Possible duplicate of [NASM Assembly convert input to integer?](http://stackoverflow.com/questions/19309749/nasm-assembly-convert-input-to-integer) – Ruud Helderman Dec 05 '16 at 14:39

1 Answers1

0

Solved, maybe someone else will need this.

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(int argc, char** argv)
{
    int Lenght;
    int pirmasSkaicius = 0;
    int antrasSkaicius = 0;

    int treciasSkaicius = 0;
    int ketvirtasSkaicius = 0;

    int rez = 0;
    char * numbEntered = new  char[10];
    if (argc < 2) {
        printf("No parameter: number\n");
        return(0);
    }
    numbEntered = argv[1];
    Lenght = strlen(numbEntered);
    cout << "Lenght: " << Lenght << endl;


    __asm {

        push eax
        push ebx
        push ecx
        push edx

        add treciasSkaicius, eax  
        add ketvirtasSkaicius, edx  
        xor eax, eax 
        xor edx, edx 

        mov ecx, numbEntered 

        mov al, byte ptr[ecx] 
        sub eax, 48 
        mov ebx, 1000 
        imul eax, ebx 
        mov pirmasSkaicius, eax 

        inc ecx 
        xor eax, eax 
        xor edx, edx 

        mov al, byte ptr[ecx] 
        sub eax, 48 
        mov ebx, 100 
        imul eax, ebx 
        mov antrasSkaicius, eax

        xor eax, eax 
        xor edx, edx 
        inc ecx 

        mov al, byte ptr[ecx] 

        sub eax, 48 
        mov ebx, 10 
        imul eax, ebx 
        mov treciasSkaicius, eax 

        inc ecx 
        xor eax, eax 
        xor edx, edx 
        mov al, byte ptr[ecx] 

        sub eax, 48 
        mov ketvirtasSkaicius, eax 
        add edx, pirmasSkaicius 
        add edx, antrasSkaicius 
        add edx, treciasSkaicius 
        add edx, ketvirtasSkaicius
        mov rez, edx 

                     pop edx
                     pop ecx
                     pop ebx
                     pop eax

    }
    cout << "Processed: " << rez << endl;
    cout << "Pieces as: " << pirmasSkaicius << " " << antrasSkaicius << " " << treciasSkaicius << " " << ketvirtasSkaicius << endl;

    system("pause");
    return 0;
}
SurisDziugas
  • 160
  • 1
  • 3
  • 12