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!
It does that because:
You load accumulator with
b
, so it's alreadyb*1
, then your loop runs 10 times and it addsb*10
to it, so you will getb*11
. Either run your loop 9 times or start with a zeroed accumulator.PS: learn to use a debugger.