2

わかりましたので、質問は簡単です。2 つのランダムなバイトがある場合、たとえば 150 (a[0]) と 215(b[0]) を追加します。明らかにそれらの合計はバイトに収まらないので、それらを追加するとオーバーフローが発生します。バイトの1つをalに格納してcbwを実行しようとしたので、同じ量が単語axでのみ表され、2番目のバイトがそれに追加されますが、理解できないことがあります動作しません。サンプルコードは次のとおりです。

data segment
  a db 150,182,211
  b db 215,214,236
data ends

code segment
start:
  mov ax,data
  mov ds,ax

  lea si,a          ; these 2 shouldn't be here since i realised eventually that
                    ; i could use
  lea di,b          ; a constant for memory addressing and not necessarily a
                    ; a register
  mov ax,0000
  mov bl,a[0]
  mov al,b[0]
  cbw
  adc bx,ax      ; so this didn't work out well

  mov ax,0000
  mov al,a[0] 
  cbw            ; convert one of the bytes into a word
  mov cx,ax      ; and save it in cx
  mov al,b[0]    
  cbw            ; also convert the other byte into the word ax
  add ax,cx      ; add the two words
                 ; and that also failed
4

1 に答える 1

7

Say, you need to add two bytes, each having a value from 0 to 255 inclusive.

You need to add those bytes and save the value of the carry flag after the addition, which will be the 9th bit of the sum.

Here's how you could do it:

mov al, byte1
mov ah, 0 ; ax = byte1
add al, byte2
adc ah, 0 ; ax = byte1 + byte2

Note, I'm using mov ah, 0 instead of cbw when extending an 8-bit value to 16 bits. cbw works if your byte is supposed to represent negative values as well as positive, IOW, if it's in the range -128 to 127 instead of 0 to 255. And you're saying you've got 150 (0x96) and 215 (0xD7), so they have to be used as unsigned or non-negative values. If you apply cbw on them anyway, you'll get: -106 (0xFF96) and -41 (0xFFD7). And that's hardly your original numbers, right?

于 2013-02-16T19:07:17.960 に答える