1

我想通过使用 32 位寄存器value1 添加到 value2 并给出 64 位以下的值(等于 16 位)。是否可以使用 2 个寄存器的空间(32+32 = 64bit)?我认为可以通过使用PTR OPERATOR来完成,但我不知道如何使用 PTR 指令。

我已经制作了添加程序。它在控制台中接受两个值并给我们结果。它只能取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
4

1 回答 1

2

您可以简单地使用CF(carry flag) 来确定添加两个整数时是否存在溢出。两个宽整数的加法进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
于 2013-08-12T18:01:50.283 回答