Register returning a hex value 1 higher than it should (8085 assembly)

112 views Asked by At

I am working on an assignment for my intro to Computer Engineering class. I am trying to write a subroutine that takes a number input from the user and then returns this number in H register.

From what I can see, it works fine with single digit inputs but when I try to continue on and add another one, it returns (input #)+1 in the H register.

Inputs will not exceed two characters and will not be greater than 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

Can anyone see what I might be doing wrong here? I hope I provided everything, but let me know if I need to clear anything up with the code.

Thanks!

1

There are 1 answers

0
Jester On BEST ANSWER

It does that because:

        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

You load accumulator with b, so it's already b*1, then your loop runs 10 times and it adds b*10 to it, so you will get b*11. Either run your loop 9 times or start with a zeroed accumulator.

PS: learn to use a debugger.