Search code examples
assemblydosx86-16

Extracting digit at end of string assembly 8086 masm


I have to extract digit stored in si register. here is my code

lea si, userInput
inc si

mov bx, [si]

mov dx, [si+bx]
add dx, 30h
mov ah, 2h 
int 21h

So i think code works fine if i put hard value in dx register like mov dx, [si+2] but if try to use mov dx, [si+bx] it does not work and does not give ouput as expected


Solution

  • Assuming your userInput points at the input structure required for DOS's buffered input function 0Ah, these are my recommandations to correct your code:

    • The second byte holds the number of inputted characters. You mistakenly retrieve this as if it were a word.
    • The input consists of characters that are 1 byte wide. You currently retrieve it as if they were 2 bytes.
    • Since the input is already made up of characters, you don't need to do any conversion on it. (The attempt mov dx, 30h) The fact that this character could represent a (numerical) digit, a letter, a punctuation mark, or anything else, doesn't change this.

    Your code then becomes:

    lea si, userInput
    inc si
    
    mov bl, [si]     ;Number of inputted characters
    mov bh, 0        ;Need to zero to be able to use the whole address-register BX next
    
    mov dl, [si+bx]  ;Retrieve the last inputted character (right before the terminating CR)
    mov ah, 02h 
    int 21h          ;Display the character