汇编语言8086使用32位reg& amp;给出64位的价值?

时间:2013-04-20 14:30:26

标签: assembly x86 masm irvine32

我希望通过使用32位寄存器 value1添加到value2,并将值设置为64位(等于16位)。是否可以使用2个寄存器(32 + 32 = 64位)的空间?我认为可以使用 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

1 个答案:

答案 0 :(得分: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