Question

So im reading the user's 8-digit input, and saving it into a variable. for example:

Enter an 8-digit hex number: 1ABC5678

So, then i loop through the 1ABC5678 hex number and subtract 48 from numbers 0-9 and subtract 55 from A-F to have a number representation of the input. but that seems to be wrong. My goal is to convert that 8-digit hex into a octal. I already know how to get from binary to octal, and thats by masking and shifting the 32bit binary digit. But my issue is that my approach to get the binary is wrong.

Im coding on Intel IA-32. x86.

UPDAT

Now i got a 32 bit binary to the variable name binary. I want to convert it to octal. Any ideas on how to shift 3by3 in order to get the right octal number?

This is my code so far:

; Now we have the Binary digits saved into the binary variable
; Lets convert into octal

octal_init:
mov     ecx, 11             ; initialize count
mov     edi, octal             ; point to start of new string
mov     eax, [binary]               ; get a character
MOV     edx, 00000000000000000000000000000000b ;Clear EDX

octal_cont:
MOV     dl, al
SHL     dl, 5
SHR     dl, 5

octal_top:
mov     [edi], dl               ; store char in new string
inc     edi                     ; update dest pointer
SHR     eax, 3
dec     ecx                     ; update char count
jnz     octal_cont                  ; loop to top if more chars
Was it helpful?

Solution

Some of the problems are:

  • the input string is newline-terminated, not null-terminated
  • some logic errors

You could write it like this:

L1_init:
mov     ecx, 8                  ; initialize count
mov     esi, buf                ; point to start of buffer
mov     edi, binary             ; point to start of new string


L1_top:
mov     al, [esi]               ; get a character
inc     esi                     ; update source pointer

cmp       al, '0'               ; Compare al with ascii '0'
...
or        dl, al                           ; Merge in the four bits just computed
loop      L1_top                          ; Loop back for next byte to convert

mov     [edi], edx               ; store char in new string

L1_end:

Note that you have already checked that you have 8 chars so no need to do it again in the loop.

OTHER TIPS

The easiest way to convert a Hex string to an int value is to use either strtol(), strtoll(), or strtoimax():

#include <inttypes.h>
#include <stdlib.h>

intmax_t     max;
long         l;
long long    ll;
const char * str = "1ABC5678";

max = strtoimax(str, NULL, 16);
l   = strtol(str, NULL, 16);
ll  = strtoll(str, NULL, 16);

Alternatively you can write the code by hand if you prefer:

int          pos;
long         l;
const char * str = "1ABC5678";

l = 0;
for(pos = 0; str[pos]; pos++)
{
    l = l << 4; // each Hex digit represents 4 bits

    // convert ASCII characters to int value
    if ((str[pos] >= '0') && (str[pos] <= '9'))
       l += (str[pos] - '0') & 0x0F;
    else if ((str[pos] >= 'A') && (str[pos] <= 'F'))
       l += (str[pos] - 'A' + 10) & 0x0F;
    else if ((str[pos] >= 'a') && (str[pos] <= 'f'))
       l += (str[pos] - 'a' + 10) 0x0F;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top