【发布时间】:2015-06-09 15:17:34
【问题描述】:
我正在执行一项任务,但不知何故被卡住了。 这是代码的一部分,一定是错误的。实际上错误的是,当程序应该打印数字时(通过过程 convertNumber,它工作得很好!),它什么也不做,也不返回。它只是没有反应,我必须手动关闭它。
read:
mov ah, 3fh ; function for reading from file
mov bx, handle ; moving file handle to BX
mov cx, 10000 ; how many bytes will be read
mov dx, buffer ; where the string will be saved
int 21h ; system call ready
mov bytesRead, 0 ; nulling my variable bytesRead, which represents how many bytes have been read
mov bytesRead, ax ; now moving the real value from AX
cmp ax, 0 ; if it is equal to 0, then there is nothing left to read
je closeFile ; close file
mov cx, 0 ; starting position
cycle:
cmp cx, bytesRead ; if I need to reload buffer
jge read
mov bx, 0 ; nulling position counter
mov bx, cx ; moving CX to BX -> mov ax, buffer[cx] was an illegal operation, so I did it this way
mov ax, buffer[bx] ; getting a character from BX. position from buffer
cmp ax, 'a'
jb next ; below 'a', skip
cmp ax, 'z'
ja next ; above 'z', skip
inc count_task ; if it passed, it is between 'a' and 'z'
jmp next ; and moving on
next:
inc cx ; increase position counter (always!)
jmp cycle ; and repeat
finally:
mov ax, count_task ; move the final count to AX (needed by the procedure convertNumber)
call convertNumber ; convert number to its ASCII value (+48)
ret
error_fopen:
print fopenErr
ret
error_fclose:
print fcloseErr
ret
closeFile:
mov ah, 3eh
mov bx, handle
int 21h
jc error_fclose
jmp finally
convertNumber proc near
push si
mov buff, '$'
lea si, buff
mov cx, 10
conv:
mov dx, 0
div cx ; AX is being divided by CX, where value 10 is stored (direct dividing by 10 is not legal)
add dx, 48 ; getting ASCII value of the number (from 0 to '0')
dec si ; going backwards
mov [si], dl
cmp ax, 0
jz printNumber ; if AX is empty (0), print the number
jmp conv ; else loop
printNumber:
mov ah, 9 ; service to print string
mov dx, si ; position of my string (number)
int 21h
pop si
ret
convertNumber endp
我希望你能理解这段代码中发生了什么,如果没有,我可以解释给你。如果有任何帮助,我将不胜感激!
【问题讨论】:
-
你有很好的标签名称,但你没有评论每一行。如果您对源代码的每一行都提供简短的描述性注释,您将收到更加热情的回应,并获得更多的帮助。
-
你为什么在
mov ax, buffer[bx]之前mov bx,0然后做mov bx,cx?顺便说一句,这将是loop和lodsb指令的理想应用程序。你应该检查出来。 :) 正如@User.1 所建议的,出于充分的理由,您应该评论您的说明。有时在这样做的过程中,你会发现问题。 -
使用调试器单步执行代码,看看哪里出错了。
-
@User.1 是的,对不起,我的错,我现在几乎每一行都评论了
-
好的,可能依赖于汇编程序。如果大小相同,则无需在加载值之前将
0加载到某个位置。所以mov ax, 0后跟mov ax,something可以是mov ax, something。你应该为你的新任务提出一个新问题。您希望如何打印职位?就像输出中的每行数字一样?
标签: file assembly count letters