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

时间:2018-06-18 17:08:15

标签: nasm x86-16 dosbox

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

1 个答案:

答案 0 :(得分: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

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