data segment
num1 db 0;3 dup(?)
num2 db 0;3 dup(?)
data ends
code segment 'code'
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov si,offset num1
输入一组数
mov ah,01h
int 21h
sub al,'0';;
mov dl,al
mov cl,3
shl al,cl
shl dl,1
add al,dl
mov [si],al
mov ah,01h
int 21h
sub al,'0'
add [si],al
mov ah,01h
int 21h
mov bx,offset num2
再输入一组数携晌
mov ah,01h
int 21h
sub al,'0'
mov dl,al
mov cl,3
shl al,cl
shl dl,1
add al,dl
mov [bx],al
mov ah,01h
int 21h
sub al,'0'
add [bx],al
mov ah,02h
mov dl,0dh
int 21h
mov dl,0ah
int 21h
把两组数求和
mov al,[bx]
add al,[si];al=sum
xor ah,ah
mov cl,100
div cl
mov bx,ax
test al,al
jz @f
add al,'0'
mov dl,al
mov ah,02h
int 21h
@@:
mov al,bh
xor ah,ah
mov cl,10
div cl
mov bx,ax
add al,'0'
mov dl,al
mov ah,02h
int 21h
add bh,'0'
mov dl,bh
mov ah,02h
int 21h
mov ah,4ch
int 21h
code ends
end start
input example:
12 34
or
02 08
十进制转二旁茄进制代码:
void dectobin( int n ){
int result=0,k=1,i,temp;
temp = n;
while(temp){
i = temp%2;
result = k * i + result;
k = k*10;
temp = temp/2;
}
printf("%d\n", result);
}
算法分析:
在实现模2取余后需要逆序输出,这个地方通过不断乘以10来实现后一运隐察位比前一位的位数多10,不需要再通过数组或者逆序函数来实现。
DATA SEGMENT
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:MOV AX,DATA
MOV DS,AX
MOV AH,1
INT 21H
SUB AL,30H
MOV BL,AL
MOV AH,2
MOV DL,13
INT 21H
MOV AH,2
MOV DL,10
INT 21H
MOV AH,1
INT 21H
SUB AL,30H
ADD AL,BL
AAM
PUSH AX
POP BX
MOV AH,2
MOV DL,13
INT 21H
MOV AH,2
MOV DL,10
INT 21H
MOV AH,2
MOV DL,BH
ADD DL,30H
INT 21H
MOV AH,2
MOV DL,BL
ADD DL,30H
INT 21H
MOV AH,4CH
INT 21H
CODE ENDS
END START