Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

assembly - NASM - How do you move an 8-bit register into a full 32-bit register?

I am writing NASM assembly code, and have to do some indexed addressing. I have the index stored in $al, but x86 won't let you use $al as an index register, and I'm already using $bl, so I cant use $bx. So I need to put the byte I have in $al into a 32-but register such as $ecx, however, when I try, it throws an invalid combination of opcode and operand error. Is there any way to do this?

    sub     al, 97                  ; char - 97

    push    ecx                     ; b/c al cant be used as indexing register
    mov     ecx, al                 ; move byte in al into ecx

    mov     bl, [table + ecx]       ; value_at(first_table_addr + char) -> bx

    pop     ecx
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the MOVZX instruction:

movzx ecx, al  ; move byte to doubleword, zero-extension

There's also MOVSX if you want the value in al to be treated as signed.

Zero-extention means the upper bits of the destination operand will be set to zero, while sign-extension means the upper bits of the destination operand will be set to the sign bit of the source operand. Some examples:

mov al,0x7F
movzx ebx,al   ; ebx = 0x0000007F
movsx ebx,al   ; ebx = 0x0000007F

mov al,0x80
movzx ebx,al   ; ebx = 0x00000080
movsx ebx,al   ; ebx = 0xFFFFFF80

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...