首页 > 解决方案 > 十进制到八进制和六进制的转换(同时输出)- 汇编

问题描述

我有一个应该接受十进制并将其转换为八进制和六进制的汇编程序。目前,它可以转换两者,但它会提示用户输入所需的值以转换两次。我应该只提示用户一次,然后打印两个输出。我想将输入的值保存在一个变量中,并将其传递给两个转换的函数。

这是我之前的代码:

.model small
.stack
.data
    CR equ 13
    lf equ 10
    Introduction db cr,lf,09H,">TEST<$"
    Students db cr,lf,09H,"    >TEST<$"
    Lab db cr,lf,09H,09H,09H,">TEST<$"
    Section db cr,lf,09h,09h,09h,09h,">TEST<$"
    Oct db cr,lf,0Ah,09H,">Octal Decimal<$"
    Hexa db cr,lf,0Ah,09H,">Hexadecimal<$"
    message1 db cr,lf,09H,"Enter a decimal number in a range of 0-65535: $"
    message3 db cr,lf,0Ah,09H,09H, "Equivalent octal number is:$"
    message4 db cr,lf,0Ah,09H,09H, "Equivalent hexadecimal number is:$"
    Again db cr,lf,0Ah,09h,"Do you want to try again?(y/n)$"
    Bye db cr,lf,0Ah,09H,09H,09H," >Thank you and Goodbye!<$"

oBuffer             dw 6
                dw 0
                    dw 6 dup(0)

oMultiplier         dw 0ah

hBuffer             dw 6
                    dw 0
                    dw 6 dup(0)

hMultiplier         dw 0ah
.code
org 100h

start:
    mov cx,0000h
    mov dx,184fh
    mov bh,21h
    mov ah,6
    int 10h

    mov dh,0
    mov dl,0
    mov bh,0
    mov ah,2
    int 10h

    mov ax,@data
    mov ds, ax

    lea dx,Introduction
    mov ah,9
    int 21h

    lea dx,Students
    mov ah,9
    int 21h

    lea dx,Lab
    mov ah,9
    int 21h

    lea dx,Section
    mov ah,9
    int 21h
    jmp Octal   
try:        
lea dx,Again
mov ah,9
int 21h

xor ah,ah
int 16h
mov bl,al

mov dl,al
mov ah,02h
int 21h

cmp bl,'Y'
je start
cmp bl,'y'
je start
cmp bl,'N'
je stop
cmp bl,'n'
je stop
stop :
lea dx,Bye
mov ah,9
int 21h

mov ax,4C00h 
int 21h 
Octal:

lea dx,Oct
mov ah,9
int 21h

mov dx,offset message1
mov ah,09h
int 21h

mov ah, 0ah
lea dx, oBuffer
int 21h

mov si, offset oBuffer + 2
mov cl, byte ptr [si-1]
mov ch, 00h
OctSubtract :
mov al, byte ptr [si]
cmp al, 30h
jnb OctCont1
jmp stop
OctCont1 :
cmp al, 3ah
jb OctCont2
jmp stop

OctCont2 :
sub al, 30h
mov byte ptr [si], al
inc si
loop OctSubtract
mov si, offset oBuffer + 2
mov cl, byte ptr [si-1]
mov ch, 00h
mov ax, 0000h
OctCalc :
mul oMultiplier
mov bl, byte ptr [si]
mov bh, 00h
add ax, bx
inc si
loop OctCalc
mov si, offset oBuffer + 2
mov bx, ax
mov dx, 0000h
mov ax, 8000h
OctOctConvert :
mov cx, 0000h
OctConv :
cmp bx, ax
jb OctCont3
sub bx, ax
inc cx
jmp OctConv
OctCont3 :
add cl, 30h
mov byte ptr [si], cl
inc si
mov cx, 0008h
div cx
cmp ax, 0000h
jnz OctOctConvert
mov byte ptr [si], '$'

lea dx,message3
mov ah,9
int 21h
mov dx, offset oBuffer + 2
int 21h

Hex:
    lea dx,Hexa
    mov ah,9
    int 21h
    lea dx,message1
    mov ah,9
    int 21h

    mov ah, 0ah
    lea dx, hBuffer
    int 21h

    mov si, offset hBuffer + 2
    mov cl, byte ptr [si-1]
    mov ch, 00h
hexsubtract :
    mov al, byte ptr [si]
    cmp al, 30h
    jnb hexcont1

    jmp stop
hexcont1 :
    cmp al, 3ah
    jb hexcont2

    jmp stop
hexcont2 :
    sub al, 30h
    mov byte ptr [si], al

    inc si
    loop hexsubtract

    mov si, offset hBuffer + 2
    mov cl, byte ptr [si-1]
    mov ch, 00h
    mov ax, 0000h
hexcalc :
    mul hMultiplier
    mov bl, byte ptr [si]
    mov bh, 00h
    add ax, bx
    inc si
    loop hexcalc

    mov si, offset hBuffer + 2
    mov bx, ax
    mov dx, 0000h
    mov ax, 1000h
hexconvert :
    mov cx, 0000h
conv :
    cmp bx, ax
    jb hexcont3
    sub bx, ax
    inc cx
    jmp conv
hexcont3 :
    cmp cx, 0ah
    jb hexcont4
    add cl, 37h
    jmp hexcont5
hexcont4 :
    add cl, 30h
hexcont5 :
    mov byte ptr [si], cl
    inc si
    mov cx, 0010h
    div cx
    cmp ax, 0000h
    jnz hexconvert

    mov byte ptr [si], '$'
    lea dx,message4
    mov ah,9
    int 21h
    lea dx,hBuffer + 2
    int 21h
    jmp Try

end start

这是我之前的输出:

在此处输入图像描述

我当前的代码:

.model small
.stack
.data
    CR equ 13
    lf equ 10
    Introduction db cr,lf,09H,">This Program converts Decimal into Hexadecimal and Octal Decimal<$"
    Students db cr,lf,09H,"    >TEST<$"
    Lab db cr,lf,09H,09H,09H,">TEST<$"
    Section db cr,lf,09h,09h,09h,09h,">TEST<$"
    Oct db cr,lf,0Ah,09H,">Octal Decimal<$"
    Hexa db cr,lf,0Ah,09H,">Hexadecimal<$"
    message1 db cr,lf,09H,"Enter a decimal number in a range of 0-65535: $"
    message3 db cr,lf,0Ah,09H,09H, "Equivalent octal number is:$"
    message4 db cr,lf,0Ah,09H,09H, "Equivalent hexadecimal number is:$"
    Again db cr,lf,0Ah,09h,"Do you want to try again?(y/n)$"
    Bye db cr,lf,0Ah,09H,09H,09H," >Thank you and Goodbye!<$"

Buffer          dw 6
                dw 0
                    dw 6 dup(0)

Multiplier      dw 0ah

.code
org 100h

start:
    mov cx,0000h
    mov dx,184fh
    mov bh,21h
    mov ah,6
    int 10h

    mov dh,0
    mov dl,0
    mov bh,0
    mov ah,2
    int 10h

    mov ax,@data
    mov ds, ax

    lea dx,Introduction
    mov ah,9
    int 21h

    lea dx,Students
    mov ah,9
    int 21h

    lea dx,Lab
    mov ah,9
    int 21h

    lea dx,Section
    mov ah,9
    int 21h
    jmp Octal   
try:        
lea dx,Again
mov ah,9
int 21h

xor ah,ah
int 16h
mov bl,al

mov dl,al
mov ah,02h
int 21h

cmp bl,'Y'
je start
cmp bl,'y'
je start
cmp bl,'N'
je stop
cmp bl,'n'
je stop
stop :
lea dx,Bye
mov ah,9
int 21h

mov ax,4C00h 
int 21h 
Octal:

lea dx,Oct
mov ah,9
int 21h

mov dx,offset message1
mov ah,09h
int 21h

mov ah, 0ah
lea dx, Buffer
int 21h

mov si, offset Buffer + 2
mov cl, byte ptr [si-1]
mov ch, 00h
OctSubtract :
mov al, byte ptr [si]
cmp al, 30h
jnb OctCont1
jmp stop
OctCont1 :
cmp al, 3ah
jb OctCont2
jmp stop

OctCont2 :
sub al, 30h
mov byte ptr [si], al
inc si
loop OctSubtract
mov si, offset Buffer + 2
mov cl, byte ptr [si-1]
mov ch, 00h
mov ax, 0000h
OctCalc :
mul Multiplier
mov bl, byte ptr [si]
mov bh, 00h
add ax, bx
inc si
loop OctCalc
mov si, offset Buffer + 2
mov bx, ax
mov dx, 0000h
mov ax, 8000h
OctOctConvert :
mov cx, 0000h
OctConv :
cmp bx, ax
jb OctCont3
sub bx, ax
inc cx
jmp OctConv
OctCont3 :
add cl, 30h
mov byte ptr [si], cl
inc si
mov cx, 0008h
div cx
cmp ax, 0000h
jnz OctOctConvert
mov byte ptr [si], '$'

lea dx,message3
mov ah,9
int 21h
mov dx, offset Buffer + 2
int 21h

Hex:
    mov si, offset Buffer + 2
    mov cl, byte ptr [si-1]
    mov ch, 00h
 hexsubtract :
    mov al, byte ptr [si]
    cmp al, 30h
    jnb hexcont1

    jmp stop
 hexcont1 :
    cmp al, 3ah
    jb hexcont2

    jmp stop
 hexcont2 :
    sub al, 30h
    mov byte ptr [si], al

    inc si
    loop hexsubtract

    mov si, offset Buffer + 2
    mov cl, byte ptr [si-1]
    mov ch, 00h
    mov ax, 0000h
 hexcalc :
    mul Multiplier
    mov bl, byte ptr [si]
    mov bh, 00h
    add ax, bx
    inc si
    loop hexcalc

    mov si, offset Buffer + 2
    mov bx, ax
    mov dx, 0000h
    mov ax, 1000h
hexconvert :
    mov cx, 0000h
conv :
    cmp bx, ax
    jb hexcont3
    sub bx, ax
    inc cx
    jmp conv
hexcont3 :
    cmp cx, 0ah
    jb hexcont4
    add cl, 37h
    jmp hexcont5
hexcont4 :
    add cl, 30h
hexcont5 :
    mov byte ptr [si], cl
    inc si
    mov cx, 0010h
    div cx
    cmp ax, 0000h
    jnz hexconvert

    mov byte ptr [si], '$'
    lea dx,message4
    mov ah,9
    int 21h
    lea dx,Buffer + 2
    int 21h
    jmp Try

end start

这是我当前的输出: 在此处输入图像描述

标签: assemblyhexdosx86-16octal

解决方案


推荐阅读