【问题标题】:Not loading the correct value from memory onto a register in 8086 assembly没有将正确的值从内存加载到 8086 程序集中的寄存器中
【发布时间】:2013-06-16 19:12:13
【问题描述】:

我是 8086 汇编的新手,请原谅我草率的代码和可能不必要的行,我是自学成才的。这段代码是我正在制作的另一个程序的片段,它需要用户输入数字。这些特定的行接受输入,然后生成计算机实际可以使用的数字。例如,取 5、4 和 3 并将这些数字“编译”成 543。

问题出现在第 59 行,我尝试将一个数字从内存加载回寄存器 bx,在这种情况下,不是加载正确的数字,如 40(来自 543 ex。)它只是加载一个 1 .

第 59 行之后的一些代码甚至可能无法运行,因为我被卡在那里了。

我可能没有使用正确的寄存器,但同样,我是自学成才,很难在网上找到易于理解的语法信息。

org  100h

mov si, 100d

input1:
    mov ah, 1h      ;input char
    int 21h
    push ax
    sub al, 30h     ;convert ascii to integer
    mov dl, al      ;put char into dl to be read
    mov [si], al    ;save char to ram for later
    mov ah, 2h      ;output char
    inc si          ;to save on next location in mem 
    pop ax
    cmp al, 13      ;check if done
    jne input1

    dec si              ;insert terination char 
    dec si              ;decrement to save value of si for multilying by ten
    push si             ;save current si value
    inc si              ;then continue
    mov al, 24h
    mov [si], al

    pop si
    mov cx, 1

    compileNum1:
        mov ax, 0
        mov bx, 0
        mov dx, 0    
    .fixNum:
        mov al, [si]   ; load last num into ax to be multiplied by 10
        mul cx
        mov bp, ax
        mov [si], bp
        dec si
        mov al, 10
        mov bx, cx
        mul bl
        mov cx, ax           
        cmp si, 99d
        jne .fixNum

    mov si, 100d       ;starts number addition

    mov ax, [si]       ;loads first number
    inc si             ;prepares second
    mov bx, [si]       ;loads second

    cmp bx, 24h        ;checks if there was only 1 number
    je .terminate1     ;if there was, goto terminate

    add ax, bx         ;else add them together

    .stloop1:
        inc si         ;prepares for third, fourth etc
        mov bx, [si]   ;loads it

        cmp bx, 24h    ;checks if numbver is 3 digts ot more long (depends on loop)
        je .terminate1 ;terminate if so

        add ax, bx     ;add them together, store in ax

    .terminate1:
        mov [100d], ax





mov ax, 0   ;clear screen
int 10h 


mov ah, 2h  ;print char
int 21h

mov ah, 0   
int 16h
ret

感谢您的帮助!

【问题讨论】:

    标签: assembly binary hex x86-16


    【解决方案1】:

    我通常用来“从键盘获取数字”的算法——实际上是“从键盘获取文本并将其转换为数字”,如您所见,如下所示:

    预循环:

    将“到目前为止的结果”寄存器设置为零。

    将 16 位寄存器设置为 10 以用作乘法器

    循环:

    获取文本字符。您可以一次获取一个,就像您正在做的那样,或者从用户放置它的输入缓冲区一次加载一个。

    确保您有一个十进制数字 - 那些讨厌的用户会输入任何该死的东西!您可能想要接受“+”和/或“-”。我会在这里注意 13 (CR) 或其他终止字符,而不是像你正在做的那样在最后(你真的不想将“转换为数字”算法应用于 CR)。如果有的话,你可以对“垃圾输入”做任何你想做的事情。

    最简单的事情就是假设一个行为良好的用户。我通常只返回到目前为止我得到的数字——即使是零。一个礼貌的错误信息,让他们重试是最好的。

    一旦你得到一个有效的十进制数字,从字符中减去'0' - 请注意这是字符“0”,而不是数字 0。如果你愿意,你可以称它为 48 或 30h,但我认为“0”更明显的是“它的用途”。

    然后将“目前的结果”乘以 10。

    然后添加从当前数字获得的数字。这可能有点棘手。我们不能add ax, bl。我们可以add ax, bx,但我们必须确保bh 为零! (更改寄存器以满足您的目的)

    重复直到我们找到那个 CR(或其他终止字符)。就是这样,数字在“迄今为止的结果”中。

    希望这会有所帮助。你走在正确的轨道上,但我认为它比它需要的更复杂。感谢您到目前为止自己解决了这个问题!

    当谈到“数字到文本”部分时,我看到了一位常客前几天在这里发布的一个非常甜蜜、简单的示例。我记性不好。 如果需要,您可能会找到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      • 2015-09-25
      • 2021-11-26
      • 2023-03-30
      相关资源
      最近更新 更多