为什么在汇编语言中输出文本时文本会重叠在一起?

huangapple go评论52阅读模式
英文:

Why does the text flow one on top of the other when outputting the text in the assembly language?

问题

Task: 写一个计算机程序,首先要求输入您的名字,输入名字后,要求输入姓氏,然后输出:Hello, 姓氏 名字。

但是当输出名字时,它出现在消息的开头(Hello)。可能的原因是什么?如果只显示姓氏,一切都正常。

org 100h
jmp start

first_name db 255,255,255 dup("$")
last_name db 255,255,255 dup("$")
msg db "Enter first name: $"
msg2 db 10,13,"Enter last name: $"
hello db 10,13, "Hello, $"

start:
; 显示“Enter first name”消息
mov ah, 09h
mov dx, offset msg
int 21h

; 从用户读取名字
mov ah, 0Ah
lea dx, first_name
int 21h

; 显示“Enter last name”消息
mov ah, 09h
mov dx, offset msg2
int 21h

; 从用户读取姓氏
mov ah, 0Ah
lea dx, last_name
int 21h

; 显示“Hello”消息
mov ah, 09h
mov dx, offset hello
int 21h

; 显示姓氏
mov ah, 09h
lea dx, last_name
add dx, 2h
int 21h

; 显示名字
mov ah, 09h
lea dx, first_name
add dx, 2h
int 21h

; 退出程序
mov ax, 4C00h
int 21h

我是新手汇编语言,所以我在YouTube上观看了视频并阅读了文章,但我什么也不懂。

英文:

Task: Write a com-program that first asks for your first name, after entering the name, asks for the last name, and then outputs: Hello, LAST NAME ENTERED FIRST NAME ENTERED.

But when I output the name, it appears at the beginning of the message (Hello). What could be the reason? If you display only the last name, everything is fine

[enter image description here](https://i.stack.imgur.com/T94zr.png)

org 100h
jmp start

first_name db 255,255,255 dup("$")
last_name db 255,255,255 dup("$")
msg db "Enter first name: $"
msg2 db 10,13,"Enter last name: $"
hello db 10,13, "Hello, $"

start:
; display "Enter first name" message
mov ah, 09h
mov dx, offset msg
int 21h

    ; read first name from user
    mov ah, 0Ah
    lea dx, first_name
    int 21h
    
    ; display "Enter last name" message
    mov ah, 09h
    mov dx, offset msg2
    int 21h
    
    ; read last name from user
    mov ah, 0Ah
    lea dx, last_name
    int 21h
    
    ; display "Hello" message
    mov ah, 09h
    mov dx, offset hello
    int 21h
    
    ; display last name
    mov ah, 09h
    lea dx, last_name
    add dx, 2h
    int 21h
    
    ; display first name
    mov ah, 09h
    lea dx, first_name
    add dx, 2h
    int 21h
    
    ; exit program
    mov ax, 4C00h
    int 21h

I'm new to assembler, so I watched videos on YouTube and read articles, but I didn't understand anything

答案1

得分: 2

first_name db 255,255,255 dup("$")

使用**$**字符预加载缓冲区始终都是一个不好的迹象!这对于想要在他们的程序中采取捷径的人来说是一个明显的迹象。

在使用DOS.BufferedInput函数0Ah时,在输入名称后,您必须按<kbd>Enter</kbd>键。此键的ASCII代码将添加到缓冲区。例如,在输入您的名称后,缓冲区会像这样:

255, 5, A, p, t, e, m, 13, $, $, $, $, ...
====================

因此,使用DOS.PrintString函数09h打印的内容是我为您划线的部分。它包含一个回车符(13),将光标移到屏幕的左侧。逻辑上,接下来的输出将覆盖此部分。

在https://stackoverflow.com/questions/47379024/how-buffered-input-works 中了解有关DOS.BufferedInput函数0Ah的信息。

解决方案

因为在屏幕上有2个连续的名称,所以将结束最后一个名称的13替换为空格字符是有意义的,这样最后一个名称和第一个名称之间会有一些分隔:

; 显示姓氏
lea si, last_name
inc si
mov bl, [si] ; 如果输入了姓氏"Aptem",这将是5
mov bh, 0
inc si
mov byte [si+bx], " " ; 用32替换13
mov dx, si
mov ah, 09h
int 21h

英文:

> first_name db 255,255,255 dup("$")

It is always a bad sign to see a buffer getting preloaded with $ characters! It is tell-tale to someone wanting to cut corners in their program.

When using the DOS.BufferedInput function 0Ah, after entering the name you have to push the <kbd>Enter</kbd> key. The ASCII code for this key gets added to the buffer. eg After inputting your name, the buffer would look like:

255, 5, A, p, t, e, m, 13, $, $, $, $, ...
        ====================

So what you then later print with the DOS.PrintString function 09h is the part that I underlined for you. It contains a carriage return (13) that brings the cursor back to the left side of the screen. Logically it follows that the next output will overwrite this.

Read about the DOS.BufferedInput function 0Ah in https://stackoverflow.com/questions/47379024/how-buffered-input-works.

Solution

Because you have 2 names that follow each other on the screen, it will make sense to replace the 13 that ends the last name with a space character, so there's some separation between the last name and the first name (in that order):

; display last name
lea  si, last_name
inc  si
mov  bl, [si]            ; This is 5 if you inputted last name &quot;Aptem&quot;
mov  bh, 0
inc  si
mov  byte [si+bx], &quot; &quot;   ; Replace 13 by 32
mov  dx, si
mov  ah, 09h
int  21h

huangapple
  • 本文由 发表于 2023年2月8日 19:03:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定