0

我正在为我的计算机工程课程简介做作业。我正在尝试编写一个子程序,它从用户那里输入一个数字,然后在 H 寄存器中返回这个数字。

据我所见,它适用于单个数字输入,但是当我尝试继续添加另一个数字时,它会在 H 寄存器中返回 (input #)+1。

输入不会超过两个字符,也不会大于 20。

readN:  ; This subroutine reads a number digit from the user 
        ; and returns this number in H
        ; Inputs: none
        ; Outputs: H register

        push    b
        push    psw
        push    h          ; store registers in stack

        mvi     b,0        ; Zero out the B register

        lxi     d,mess4    ; "Enter number: $"
        call    bdos       ; bdos = 0005

nextN:  mvi     c,1        ; C = 1 --> Read character
        call    bdos
        cpi     cr         ; cr = 0Dh (carriage return)
        jz      lastN      ; if input was carriage return --> go to lastN

        mvi     h,10       ; set up H register for multiplication 
        sui     '0'        ; subtract ASCII 0 from input, leaving the numerical value
        mov     e,a        ; store accumulator in E register
        mov     a,b        ; bring B register (existing number) to accumulator

mult:   add     b          
        dcr     h          ; decrements multiplication tracker 
        jnz     mult       ; if h != 0 --> redo addition

        add     e          ; add E register (new input) to old input*10
        mov     b,a        ; store result in b

        jmp     nextN      ; redo input

lastN:  pop     h
        mov     h,b
        pop     psw
        pop     b
        ret

谁能看到我在这里可能做错了什么?我希望我提供了一切,但如果我需要用代码清除任何东西,请告诉我。

谢谢!

4

1 回答 1

1

这样做是因为:

        mov     a,b        ; bring B register (existing number) to accumulator
mult:   add     b          
        dcr     h          ; decrements multiplication tracker 
        jnz     mult       ; if h != 0 --> redo addition

你用 加载累加器b,所以它已经是b*1,然后你的循环运行 10 次并添加b*10到它,所以你会得到b*11。要么运行你的循环 9 次,要么从一个归零的累加器开始。

PS:学习使用调试器。

于 2015-06-19T10:34:30.020 回答