高分!紧急!汇编 输入字符串,去空格后反向输出

我用汇编写了一段输入字符串去空格后反向输出的程序,但不知道为什么没有输出:
.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

温馨提示:内容为网友见解,仅供参考
第1个回答  2010-06-07
建议直接到相关的专业网站去咨询吧,这里关注的较少。
第2个回答  2010-06-08
;符合题目要求
CODE SEGMENT
ASSUME CS:CODE,DS:CODE
START:
push cs
pop ds
mov dx,offset message
mov ah,9 ;提示
int 21h
mov dx,offset buffer
mov ah,0ah ;接收一个字符串
int 21h
mov ax,0e0dh
int 10h
mov al,0ah
int 10h
mov ax,buffer
mov cl,ah
mov ch,0
jcxz quit
xor bp,bp
mov cx,cx
mov si,offset string
mov ah,0eh
nx1: lodsb
cmp al,20h ;抽去其中的空格
jz skip
push ax
inc bp
skip:
loop nx1
mov cx,bp
nx2:
pop ax
int 10h ;按相反的顺序显示
loop nx2
quit:
mov ah,7
int 21h
mov ah,4ch
int 21h

message db 13,10,9,'Input a string: $'
buffer db 255,0
string db 255 dup (0)
CODE ENDS
END START
第3个回答  2010-06-07
自己加一个专业的群问一下 那里有很多高手可以给你解答
相似回答