rar*_*dea 2 x86 assembly x86-16
好的,问题很简单.如果我有2个随机字节,比如150(a [0])和215(b [0]),我想添加它们.显然他们的总和不适合一个字节,所以如果我添加它们我会得到一个溢出.我已经尝试在al中存储其中一个字节并执行cbw,这样我就会在单词ax上表示相同的数量,并将第二个字节添加到那个,但是有些东西我无法理解,因为它不起作用.这是一个示例代码:
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
Run Code Online (Sandbox Code Playgroud)
比如说,你需要添加两个字节,每个字节的值从0到255(含).
您需要添加这些字节并在添加后保存进位标志的值,这将是总和的第9位.
这是你如何做到的:
mov al, byte1
mov ah, 0 ; ax = byte1
add al, byte2
adc ah, 0 ; ax = byte1 + byte2
Run Code Online (Sandbox Code Playgroud)
注意,我正在使用mov ah, 0
而不是cbw
将8位值扩展为16位.cbw
如果你的字节应该表示负值以及正值,IOW,如果它在-128到127而不是0到255的范围内,则可以工作.你说你有150(0x96)和215(0xD7) ,因此必须将它们用作无符号或非负值.cbw
无论如何你都应用它们,你会得到:-106(0xFF96)和-41(0xFFD7).那不是你的原始数字,对吗?