loops - 在emu8086中打印从1到<用户输入

标签 loops assembly emu8086

我想从用户那里获取一个数字(即 5),然后从 1 开始打印到 < 输入(即 1 2 3 4) 但我的代码不会在“4”处停止,而是循环运行到“d”

我知道循环运行 CX 次 因为在 8086 MOVZX 中不起作用,这就是为什么我首先将 AL 移至 CL,然后将 CH 归零。

正如有人提到的,问题是当我将 AL 移动到 CX 时,我没有移动值 4,而是移动了 34(ASCII 值 4),因此我的循环运行了 34 次。

现在如何将用户输入值转换为十进制并将其移至 CX。有没有办法将用户输入作为十进制值存储在 AL 中?

org 100h




MOV AH, 1  ; Get user input 
INT 21H


DEC AL ; Dec AL to satisfy the condition that it will print till < input

MOV BL,31H ; Initialize BL so that the output starts printing from 1 

MOV CL,Al ; set counter register CX 
MOV CH,00


Print:

MOV AH, 2    ; for output printing
MOV DL,0DH  ; for output printing
INT 21H      ; for output printing

MOV DL,0AH      ; for output printing
INT 21H            ; for output printing

MOV AH,2
MOV DL,BL         ; print what is in BL 
INT 21H

INC BL             ; then increment BL

LOOP Print     ; supposed to run the loop on Print what is the value in CL times

hlt

最佳答案

MOV AH, 1  ; Get user input 
INT 21H

如果您输入5,则AL寄存器将保存数字35h,这是该键的ASCII代码。您显然想要该键代表 5。您需要减去 30h (48)。

mov     ah, 01h  ; DOS.GetKey 
int     21h
sub     al, '0'
dec     al
mov     cl, al
mov     ch, 0

程序的其余部分适合从 1 到 < 输入开始打印。

关于loops - 在emu8086中打印从1到<用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55502300/

相关文章:

c - 如何在一个循环内完美地创建一个循环,同时又在另一个循环内创建一个循环?

javascript 循环推送

c - 在 C/C++ 中使用 malloc 和 free 并收到错误 HEAP CORRUPTION DETECTED

assembly - 在 TASM 理想模式下设置数据段的对齐方式

assembly - 这个8086升序代码是如何工作的?

assembly - ASM 8086 没有分区的分区

php - MySQL 将表与结果中的原始信息连接起来

delphi - 将字节值广播到 Delphi ASM 中的所有 16 个 XMM 插槽

linux - 不中断 assembly 时出现段错误

assembly - .code/.data 和 code/data 段有什么区别?