【问题标题】:8086 Write a program that takes one string from the input and inserts white space between every two letters8086 编写一个程序,从输入中获取一个字符串,并在每两个字母之间插入一个空格
【发布时间】:2018-06-18 17:08:14
【问题描述】:

8086 编写一个程序,从输入中获取一个字符串并在每两个字母之间插入一个空格

使用 nasm 和 MSDOS

我做了以下代码,但它不工作

start:        

    mov ax,data
    mov ds, ax
    mov es, ax   
    mov cx, size;cx will contain size,we need it to
                ; check if we have got 10 inputs from key board

    lea dx, Enter_string;it will display a text on screen to enter text
    mov ah, 9
    int 21h

    call get_string;input string from keyboard

    mov ax, 4ch;terminating 
    int 21h   

get_string: 
     mov si, 0; si will be used as index           
     mov bx, offset string
   get_char: 

     mov ah, 1; get a char from keyboard
     int 21h                                                   

     mov [bx][si], al; saving input in string
     inc si
     cmp si,cx;if si=7 than, no need to take more input
     jne get_char
ret

【问题讨论】:

  • 不工作怎么办?看起来它甚至不会在没有定义 Enter_string 符号的情况下进行组装。 (或者在 NASM 语法中,即使有定义也没有。使用mov dx, Enter_string,因为lea dx, [Enter_string] 更慢/更大,并且仍然是16 位绝对寻址。)无论如何,这不是minimal reproducible example。此外,如果这是一个 DOS .com 程序,则需要 org 0x100,而 mov ax, data 看起来不正确。我认为您需要一些其他语法来获取细分基础。
  • 一个明显的问题,甚至来自古老的记忆:mov ax, 4ch4c 需要放在 AH 中,而你要在 AL 中返回的值——但就目前而言,这是将 4c 放在 AL 中,00 放在 AH 中。我不记得 DOS 定义了一个函数 0,所以我不确定它现在会做什么(但几乎可以肯定不会终止)。

标签: nasm x86-16 dosbox


【解决方案1】:

。在学习时,您应该更喜欢 .COM 程序的简单性。它们已经从指向程序的所有段寄存器开始。
. DOS 退出函数需要 AH 中的函数号。
.在 NASM 上,您不使用 mov bx, offset string。只需写mov bx, string
. 输入字符串插入空格字符的任务很容易结合起来。见以下代码:

org 256               ;.COM programs have CS=DS=ES=SS

start:

mov cx, size          ;cx will contain size,we need it to
                      ; check if we have got 10 inputs from key board

mov dx, Enter_string  ;it will display a text on screen to enter text
mov ah, 9
int 21h

call get_string       ;input string from keyboard

mov ax, 4C00h         ;terminating 
int 21h   

get_string:
 push cx
 mov si, 0            ; si will be used as index           
get_char:
 mov ah, 1            ; get a char from keyboard
 int 21h                                                   
 mov ah, " "
 mov [string+si], ax  ; saving input in string PLUS THE SPACE CHARACTER
 add si, 2
 dec cx               ;if si=7 than, no need to take more input
 jnz get_char
 pop cx
 ret

请记住,您实际上并不需要最后一个空格字符。只需使用通常添加到此字符串的字符串终止符覆盖它!

【讨论】:

    猜你喜欢
    • 2012-05-19
    • 1970-01-01
    • 2015-12-12
    • 2020-03-04
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    相关资源
    最近更新 更多