The Niv
The Niv

Reputation: 135

Merge two 4-bit numbers into an 8-bit number

So the problem is:
There are 2 characters.
I need to build a 8-bit number that is built like this:
the left 4 bits from the first number, the right 4 bits from the second number

Edit: if the number will be placed in al, the bits should be like this:

al bits 0 to 3 = lowest 4 bits of the second character. al bits 4 to 7 = highest 4 bits of the first character.

I have tried to just shift right 4 bits the number to get the 4 left bits. To get the right 4 bits I tried to turn the 4 left bits of the number to 0. Then I added the 4 left bits to ax,shifted it left 4 times, and then added the left 4 bits.

    mov dl,[si]       ; the value of the character, it is inside of a char array
    shr dl,4
    add al,dl
    and dl,00001111b
    shl ax,4          ; ax value was 0
    inc si
    mov dl,[si]
    and dl,00001111b
    add al,dl
    shl ax,4       

I thought this should work but apparently it doesn't.

How can I do it?

Upvotes: 0

Views: 864

Answers (1)

Brendan
Brendan

Reputation: 37232

I need to build a 8-bit number that is built like this: the left 4 bits from the first number, the right 4 bits from the second number

I don't know if you want something like this:

    mov ax,[si]     ;al = first character, ah = second character
    shl al,4        ;al bits 4 to 7 = lowest 4 bits of first character
    shr ax,4        ;al bits 0 to 3 = lowest 4 bits of first character, al bits 4 to 7 = lowest 4 bits of second character

..or something like this:

    mov ax,[si]     ;al = first character, ah = second character
    and ax,0xF00F   ;al bits 0 to 3 = lowest 4 bits of first character, ah bits 4 to 7 = highest 4 bits of second character
    or al,ah        ;al bits 0 to 3 = lowest 4 bits of first character, al bits 4 to 7 = highest 4 bits of second character

..or something like this:

    mov ax,[si]     ;al = first character, ah = second character
    and ax,0x0FF0   ;al bits 4 to 7 = highest 4 bits of first character, ah bits 0 to 3 = lowest 4 bits of second character
    or al,ah        ;al bits 0 to 3 = lowest 4 bits of second character, al bits 4 to 7 = highest 4 bits of first character

..or something else.

Upvotes: 1

Related Questions