我用汇编写了一段输入字符串去空格后反向输出的程序,但不知道为什么没有输出:
.model small
.386
DATA SEGMENT
buf db 100, 0 ,100 DUP (0)
msg1 db 'Please input a string: ',0DH,0Ah,'$'
crlf db 0Dh,0Ah,'$'
leng db 0
DATA ENDS
STACK SEGMENT
DB 64 DUP (?)
STACK ENDS
CODE SEGMENT
assume cs:CODE,ds:DATA,ss:STACK
START:
mov ax,DATA
mov ds,ax
lea dx,msg1 ;输出提示文字
mov ah,09h
int 21h
lea dx,buf
mov ah,0ah ;接受字符串
int 21h
mov dx,offset crlf
mov ah,09h ;输出字符串中断
int 21h
call count
call nospace;把这行去掉之后是可以实现反向输出的,当然不能 去空格
lea dx,buf
mov bl,leng;新字符串长度送到bl
mov bh,00h
cmp bx,0000h
jnz l1
jz EXIT
L1:
mov dl,buf+1[bx] ;把最后一位送到dl
mov ah,02h ;输出一个字符中断
int 21h
dec bx ;倒着输出字符
jnz l1
EXIT:
mov AH,4CH
int 21H
count:
xor dx,dx
xor cx,cx
lea di,buf
push di
mov al,0
lop:
cmp al,[di]
je done
inc dl
inc di
loop lop
done:
mov leng,dl;把数组长度存至leng
pop di
ret
nospace:
lea di,buf
push di
mov ax,20h
mov cl,leng
add di,2
cld
repne scasw
je delete
pop di
delete:
jcxz del
next:
mov bx,[di]
mov [di-2],bx
add di,2
loop next
del:
pop di
dec leng
dec word ptr [di]
ret
CODE ENDS
END START
请各位看看有什么问题呢?特别是去空格的那部分,还是其他的设置错误?
非常感谢三楼的回答 但有了空格之后最后一个字符不能输出而且有的空格也不能消除 能不能再请各位帮忙看一下呢?十分感谢~
我运行的没有错啊,
例如输入abcd efg,中间有3个空格,它会输出gfedcba
修改后的代码全给你了,就算有错应该不是nospace的吧。
;输入字符串去空格后反向输出的程序
.model small
.386
DATA SEGMENT
buf db 100, 0 ,100 DUP (0)
msg1 db 'Please input a string: ',0DH,0Ah,'$'
crlf db 0Dh,0Ah,'$'
leng db 0
DATA ENDS
STACK SEGMENT
DB 64 DUP (?)
STACK ENDS
CODE SEGMENT
assume cs:CODE,ds:DATA,es:DATA,ss:STACK
START:
mov ax,DATA
mov ds,ax
mov es,ax
lea dx,msg1 ;输出提示文字
mov ah,09h
int 21h
lea dx,buf
mov ah,0ah ;接受字符串
int 21h;
mov dx,offset crlf
mov ah,09h ;输出字符串中断
int 21h
call count
call nospace;把这行去掉之后是可以实现反向输出的,当然不能 去空格
lea dx,buf
mov bl,leng;新字符串长度送到bl
mov bh,00h
cmp bx,0000h
jnz L1
jz EXIT
L1:
mov dl,buf+1[bx] ;把最后一位送到dl
mov ah,02h ;输出一个字符中断
int 21h
dec bx ;倒着输出字符
jnz l1
EXIT:
mov AH,4CH
int 21H
count:
xor dx,dx
xor cx,cx
lea di,buf
push di
mov al,0
lop:
cmp al,[di]
je done
inc dl
inc di
loop lop
done:
mov leng,dl;把数组长度存至leng
pop di
ret
nospace:
push si
push di
push ax
push bx
push cx
lea si,buf
add si,2 ;读指针si
mov di,si ;写指针di
xor ch,ch
mov cl,[si-1] ;字符数组长度
cld
xor bl,bl ;新的字符长度
L2:
lodsb ;读取字符
cmp al,' '
je L3 ;如果是空格,跳过保存操作
stosb ;保存字符
inc bl ;记录长度
L3:
loop L2
mov leng,bl
pop cx
pop bx
pop ax
pop di
pop si
ret
CODE ENDS
END START