我得到了一個用匯編撰寫程式的作業,該程式可以在不使用neg
ormul
指令的情況下將數字乘以 -1 。我嘗試使用shl
,shr
但由于某種原因無法使其作業。有人知道我該怎么做嗎?(在二進制有符號 2 的補碼中)
這是代碼的框架:
org 100h
jmp start
start:
mov bx, 0000000000000010b
; here i need to multiply by -1 the number that in bx
mov ah, 0
int 16h
ret
uj5u.com熱心網友回復:
乘以-1 的一種方法可能是翻轉所有位,然后添加1。即讓我們在 4 位中取 7 個(為了示例,我使用 4 位)。
7(10) = 0111(2)
Flip the bits: 1000
and add 1: 1001
如您所見,我們有-7。要在匯編中執行此操作,您可以使用not
.
通過這種方式,零變為一,一變為零,翻轉后您只需添加1即可獲得它。
該shl
和shr
可以看作是乘以或除以2**numberOfBitShifted
,這就是為什么他們不能作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/343795.html