2013-04-20 152 views
1

我想加上值1到value2使用32位寄存器,並給予64位(等於16位數)下的值。是否可以使用2個寄存器(32 + 32 = 64位)的空間?我認爲這可以通過使用PTROPERATOR但我不知道如何使用PTR指令。彙編語言8086添加使用32位寄存&值在64位?

我已經制作了添加程序。它在控制檯中使用兩個值並給出結果。它只能在32位(8位)下取值。如果我們給出一個更高的值,那麼它會在控制檯中給出整數溢出錯誤。

我使用KIP.R.IRVINE鏈接庫彙編語言

我們將如何給通過使用32位寄存器,一個64位的值?我們如何使32位寄存器獲得64位值?

下面是32位加法

INCLUDE Irvine32.inc 

.data 

Addition BYTE "A: Add two Integer Numbers", 0 

inputValue1st BYTE "Input the 1st integer = ",0 
inputValue2nd BYTE "Input the 2nd integer = ",0 

outputSumMsg BYTE "The sum of the two integers is = ",0 

num1 DD ? 
num2 DD ? 
sum DD ? 

.code 

main PROC 

;----Displays addition Text----- 

mov edx, OFFSET Addition 
call WriteString 
call Crlf 
;------------------------------- 

; calling procedures here 

call InputValues 
call addValue 
call outputValue 

call Crlf 

jmp exitLabel 


main ENDP 


; the PROCEDURES which i have made is here 


InputValues PROC 
;----------- For 1st Value-------- 


call Crlf 
mov edx,OFFSET inputValue1st ; input text1 
call WriteString 

; here it is taking 1st value 
call ReadInt ; read integer 
mov num1, eax ; store the value 




;-----------For 2nd Value---------- 



mov edx,OFFSET inputValue2nd ; input text2 
call WriteString 


; here it is taking 2nd value 
call ReadInt ; read integer 
mov num2, eax ; store the value 

ret 
InputValues ENDP 




;---------Adding Sum---------------- 

addValue PROC 
; compute the sum 

mov eax, num2 ; moves num2 to eax 
add eax, num1 ; adds num2 to num1 
mov sum, eax ; the val is stored in eax 

ret 
addValue ENDP 

;--------For Sum Output Result---------- 

outputValue PROC 

; output result 

mov edx, OFFSET outputSumMsg ; Output text 
call WriteString 


mov eax, sum 
call WriteInt ; prints the value in eax 


ret 
outputValue ENDP 


exitLabel: 
exit 


END main 
+0

'PTR'不是指令和明顯64位不適合在32位。兩個32位數的總和只有33位,而額外的位在進位標誌中。 – harold 2013-04-20 18:01:12

+0

我可以使用2位32位,並給64位值,但我沒有想法如何使用它... – 2013-04-20 20:02:47

回答

2

您可以簡單地使用CF(進位標誌)的代碼,以確定是否增加了兩個整數的時候有一些溢出。兩個n-bit寬整數的加運算永遠不會超過一位,但請注意,只有在討論無符號加法時纔可以這樣做。帶有64位結果的簽名加法需要兩個64位整數。

這裏是無符號32位加一位進位的例子。

mov eax, (1<<31)|1 ;set Most-Significant Bit (MSB) to 1, what will surely cause overflow 
mov ebx, (1<<31)|1 
add eax, ebx 
jc .go    ;we need another bytes for our carry 

簽名版本:

;let eax and ebx be the numbers we want to add 
cdq ;expand 4-byte integer to 8-byte integer <-- this won't affect real value of EAX 
xchg eax, ebx ;cdq has fixed operands, change eax with ebx 
xchg edx, ecx ;... and edx with ecx 
cdq ;do the same for number that was in EBX 

add eax, ebx 
adc edx, ecx ;that 'c' on the end is important, it will add 
       ;the carry flag to the result so possible overflow will be handled 
;Result is now in EDX:EAX