#1
  1. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95

    assembler level question


    I am trying to convert a hex number into a binary one, and im having some trouble. for output, i am getting odd symbols, like smiley faces or triangles etc.. here's my code, any help greatly appreciated:

    //Ex03s07_BinOut32
    #include <iostream.h>
    #include <conio.h> // for putch()

    void HexOut32(int m)
    {
    // outputs m binary.
    char c; // tmp variable
    __asm
    {
    mov eax, m
    push ebx ; save reg so can use
    push ecx
    mov ch, 32 ; 32 binary digits to display
    HexOut32_1:
    mov CL, 1
    rol eax, CL
    mov BL, AL
    ; determine binary code for digit
    and BL, 00000001b ; mask
    add BL, 00011110b

    HexOut32_2:
    mov c, BL
    push eax ; save value
    push ecx ; save count
    }
    _putch (c);
    __asm
    {
    pop ecx ; restore counts
    pop eax ; restore value
    dec ch
    jnz HexOut32_1
    pop ecx
    pop ebx
    }
    return;
    }

    int main()
    {
    int n;
    do
    {
    cout << "\n\nEnter number (in hex, 0 to exit): ";
    cin >> hex >> n;
    if (n == 0) break;
    cout << "Here it is: "; cout.flush();
    HexOut32(n);
    } while (1);
    return 0;
    };
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Try changing this part:
    ; determine binary code for digit
    and BL, 00000001b ; mask
    add BL, 00011110b

    to this:
    ; determine binary code for digit
    and BL, 00000001b ; mask
    add BL, 00110000b

    Hope this helps!
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    U ROCK MAN!!! THANKS!! tried it and it works, but one question:

    i'm one of those types who has to know why everything works...
    why?? i thought i had to add 30 to what i had?? thanks much!:D
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    you need to add 0x30 = 48. :)
  8. #5
  9. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    oh god i just figured out what it was, i was looking at the ascii set with hex indices!! LOL, in hex 0 = 30 and 1 = 31!! thanks a lot for your help guys!! much faster than my jerk teacher who hasnt replied to my email in 2 days.:rolleyes:

IMN logo majestic logo threadwatch logo seochat tools logo