gpt4 book ai didi

assembly - 用 8086 汇编语言打印十进制值

转载 作者:行者123 更新时间:2023-12-02 11:11:44 27 4
gpt4 key购买 nike

我目前正在开发一个项目,需要提示用户输入三个输入(长度、宽度和高度),然后计算体积 (lwh)。计算完成后打印结果时遇到问题。有没有办法打印出十进制值?

.MODEL  SMALL
.STACK 100h

.DATA
l DB ?
w DB ?
h DB ?
v DB ?
M1 DB 10,13, "Please enter the length: $"
M2 DB 10,13, "Please enter the width : $"
M3 DB 10,13, "Please enter the height: $"
M4 DB 10,13, "The volume is:$"

.CODE
Main PROC
mov ax, @data ; getting data segment address
mov ds, ax ; initializing the data segment

mov dx, offset M1 ; prompting user for a value
mov ah, 09h ; writing string to STDOUT
int 21h ; BIOS routines
mov ah, 01h ; reading in from STDIN, input stored in al
int 21h
mov bl, al
sub ax,ax ; clearing ax register for the next input
sub bl, 30h
mov l, bl
sub bx,bx

mov dx, offset M2
mov ah, 09h
int 21h
mov ah, 01h
int 21h
mov bl, al
sub ax,ax
sub bl, 30h
mov w, bl
mov al, l
mul bl
mov v, al
sub ax, ax
sub bx,bx

mov dx, offset M3
mov ah, 09h
int 21h
mov ah, 01h
int 21h
sub al, 30h
mov h, al
sub bx, bx
mov bl, v
mul bx
mov v, al
sub ax, ax
sub bx,bx

mov dx, offset M4
mov ah, 09h
int 21h
sub dx, dx
mov dx, offset v
mov ah, 09h
int 21h
mov ax, 400ch ; returning control to OS
int 21h
Main ENDP
END Main

最佳答案

这可以简单地通过除法/取模来完成。例如,如果我们有一个 1 字节值要转换为十进制 - 比如说 152 - 我们可以首先将 152 除以 100,然后对其应用模 10,得到结果 1(数字中的第一个数字)

(152 / 100) % 10 = 1

然后我们可以将其保存到字符串缓冲区中以便稍后打印,同时处理下一个数字。对于下一个数字,我们重复该过程,除了除以 10 而不是 100

(152 / 10) % 10 = 5

将此结果存储在缓冲区的下一个槽中。重复此过程,直到您的值除以 1,这时您可以只使用模:

152 % 10 = 2

在伪代码中,算法看起来像这样:

buffer: byte[4]
buffer[3] = 0 ;; Null-terminate the buffer
buffer_index = 0

value: 153
divisor: 100 ;; Increase this for values larger than 999

while divisor > 0 do
buffer[buffer_index] = (value / divisor) % 10
buffer_index = buffer_index + 1
divisor = divisor / 10
repeat

print buffer

我将把汇编翻译留给你;)

关于assembly - 用 8086 汇编语言打印十进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35373086/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com