1

我必须编写一个汇编程序来计算 (a * (bc) * d) 其中 a、b、c、d 是以有符号 8 位格式存储到数据段中的变量。(我必须假设结果表示为带符号的 32 位数字)

我写了以下代码

data segment

    a db 1
    b db 9
    c db 3
    d db 4 

    x dw ?


ends

stack segment
    dw   128  dup(0)
ends

code segment
start:

    mov ax, data
    mov ds, ax
    mov es, ax


    ; what would the difference be in the following three lines if the variables where signed?
    mov al, b
    sub al,c
    mul a
    mul d


    ;treating result as a 16bit - how about signed?
    mov x,ax 





    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start

我的问题如下:除了注册表大小(al,ah,bh,bl,ecc ... 8bit),(ax,bx,cd, DX,16位)?

除了将它存储到 DW 变量之外,如何将 32 位变量与 16 位变量区别对待?

为方便起见,我在代码的相关部分中编写了上述问题的简短版本。

有人可以帮我吗?在此先感谢您的时间。

4

1 回答 1

3

In general, the contents of registers/memory are just sequences (with specific lengths) of bits, and the concept of signed/unsigned is irrelevant.

There are a few instructions where it matters whether the operators should be viewed as signed or unsigned. Some examples are:

  • Multiplication: Use MUL for unsigned operands, and IMUL for signed operands.
  • Division: Use DIV for unsigned operands, and IDIV for signed operands.
  • Conditional jumps: JA means jump if (unsigned) above, JG means jump if (signed) greater. JB means jump if (unsigned) below, JL means jump if (signed) less, and so on.

;treating result as a 16bit - how about signed?
mov x,ax 

MOV has no notion of sign, it just copies the bits from the source to the destination. All it needs to know is how many bits to copy (i.e. the size of the operands).

One exception is if you're using a widening MOV:

movzx ax,byte ptr [foo]  ; zero-extend the byte foo into ax (for unsigned numbers)
movsx ax,byte ptr [foo]  ; sign-extend the byte foo into ax (for signed numbers)

How do I treat a 32 bit variable differently than a 16 variable, apart from storing it ito a DW variable?

DW would be for the 16-bit variable. For a 32-bit variable you'd use DD.

于 2013-09-24T15:07:50.380 回答