装配程序永不停止(Turbo Debugger)

时间:2015-12-28 17:46:27

标签: assembly x86-16 tasm

我正在尝试读取一个数字并使用TASM以汇编语言在屏幕上打印 到目前为止,我有一个要阅读的宏和一个要打印的宏:

.386
DATA SEGMENT PARA PUBLIC 'DATA'
X DD 0  ;number to be read
AU DD 0   ;auxiliary variable
DATA ENDS

READ MACRO X, AUX  ;macro to read
LOCAL R, FINAL
PUSH EAX  ;save the registers we will work with
PUSH EBX
PUSH EDX

MOV EBX, 10
R:
   MOV AH, 1H
   INT 21H   ;read character
   CMP AL, 0DH  ;compare to 'Enter' key
   JE FINAL
   CMP AL,30H  ;see if it's a number above 0
   JB R
   CMP AL, 39H  ;see if it's a number below 9
   JA R
   SUB AL, 48  ;transform character to number
   XOR AH,AH
   MOV AUX,EAX 
   MOV EAX, X
   MUL EBX     ;construct the number
   ADD EAX, AUX
   MOV X, EAX
   JMP R

FINAL:
POP EDX
POP EBX
ENDM

PRINT MACRO NUMBER
LOCAL LOOPR, PRINTLN
PUSH EAX   ;save registers we will work with
PUSH EDX
PUSH EBX
PUSH SI
MOV CX, 0
MOV EAX, NUMBER
MOV EBX, 10
LOOPR:
  XOR EDX, EDX
  DIV EBX    ; eax <- eax/10, edx <- eax % 10
  ADD DL, '0'  ; edx to ASCII
  PUSH DX    
  INC CX
  CMP EAX, 0
  JNZ LOOPR
PRINTLN:
  POP DX
  MOV AH, 2H
  INT 21H
  DEC CX
  JNZ PRINTLN

  MOV DL, 13D  ;carriage return
  MOV AH, 2H
  INT 21H
  MOV DL, 10D  ;line feed
  MOV AH, 2H
  INT 21H

  POP SI
  POP EDX
  POP EAX
ENDM 


CODE SEGMENT PARA PUBLIC 'CODE'
START PROC FAR
ASSUME CS:CODE, DS:DATA
PUSH DS
XOR AX,AX
PUSH AX
MOV AX,DATA
MOV DS,AX

READ X, AU
PRINT X

RET
START ENDP
CODE ENDS
END START

如果我在Turbo Debugger中运行代码,它会读取数字,但程序会继续运行并运行,我不知道为什么。
Turbo Debugger window我的个人猜测是这与.386指令有关。我在另一个程序中遇到了类似的问题,在我删除了.386指令后,它工作了。但是在这里我需要指令,因为我使用32位寄存器。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:4)

.386的位置确实很重要。将其移至CODE SEGMENT ...行后面的一行:

...
CODE SEGMENT PARA PUBLIC 'CODE'
.386

START PROC FAR
...

READ最后需要POP EAX

...
FINAL:
POP EDX
POP EBX
POP EAX
ENDM
...

PRINT最后需要POP EBX

...
  POP SI
  POP EBX
  POP EDX
  POP EAX
ENDM
...

READ最后只执行回车(0x0D = 13d)。因此,PRINT将写入同一行,覆盖输入,您将看不到任何更改。将换行符(0x0A = 10d)添加到READ

...
FINAL:
mov dl, 10
mov ah, 2
int 21h

POP EDX
POP EBX
pop eax
ENDM
...