将两个4位数字合并为8位数字?

时间:2019-06-14 20:42:56

标签: assembly x86-16

所以问题是:
有2个字符。
我需要构建一个像这样构建的8位数字:
第一个数字的左4位,第二个数字的右4位

我试图将数字右移4位以获得4个左位。 为了获得正确的4位,我尝试将数字的4位左移为0。 然后我将剩下的4位添加到ax,将它向左移动4次,然后再添加剩下的4位。

    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       

我认为这应该可行,但显然不可行。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

  

我需要构建一个像这样构建的8位数字:   第一个数字的左4位,第二个数字的右4位

我不知道你是否想要这样的东西:

    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

..或类似这样的内容:

    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

..或类似这样的内容:

    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

..或其他内容。