在8086环境下用汇编语言编写、输入一个32位二进制数相加程序
时间: 2023-06-02 11:01:27 浏览: 181
用汇编程序实现两个32位数相加
5星 · 资源好评率100%
以下是在8086环境下用汇编语言编写、输入一个32位二进制数相加程序的代码:
```
.model small
.stack 100h
.data
msg db 'Enter a 32-bit binary number: $'
result db 'The sum is: $'
num1 db 32 dup('$')
num2 db 32 dup('$')
sum db 33 dup('$')
.code
main proc
mov ax, @data
mov ds, ax
; Input first 32-bit binary number
mov ah, 9
lea dx, msg
int 21h
mov ah, 0Ah
lea dx, num1
int 21h
; Input second 32-bit binary number
mov ah, 9
lea dx, msg
int 21h
mov ah, 0Ah
lea dx, num2
int 21h
; Convert binary numbers to decimal
call binToDec
mov bx, ax
call binToDec
add bx, ax
; Convert sum to binary
mov cx, 32 ; Counter for loop
mov di, offset sum ; Offset for sum array
mov ax, bx ; Result in AX
mov bx, 2 ; Binary base
mov dx, 0 ; Clear DX
sumLoop:
div bx ; Divide by 2
add dl, '0' ; Convert remainder to ASCII
mov [di], dl ; Store in sum array
dec di ; Decrement offset
dec cx ; Decrement counter
cmp cx, 0 ; Check if done
jne sumLoop
; Output sum in binary
mov ah, 9
lea dx, result
int 21h
mov ah, 9
lea dx, sum
int 21h
mov ah, 4ch ; Return control to DOS
int 21h
main endp
; Convert binary number in num1 to decimal
; Result in AX
binToDec proc
xor ax, ax ; Clear AX
mov cx, 32 ; Counter for loop
mov si, offset num1 ; Offset for num1 array
binLoop:
mov dl, [si] ; Get next character in num1
sub dl, '0' ; Convert to decimal
mov bx, 2 ; Binary base
mul bx ; Multiply by base (2)
add ax, dx ; Add remainder
inc si ; Increment offset
dec cx ; Decrement counter
cmp cx, 0 ; Check if done
jne binLoop
ret
binToDec endp
end main
```
这个程序会要求用户输入两个32位二进制数,然后将它们相加,最后输出结果。程序首先将输入的二进制数转换为十进制,然后将它们相加得到结果。最后,程序将结果转换为二进制并输出。
阅读全文