assemblybinaryx86-16data-conversionemu8086

Emu8086 storing 8-bit number issue


In this question I asked out why my code isn't working and I thought the issue was only when the program was trying to print out the ASCII characters but I have realized since then that there is another issue.

The task is to accept 8 characters 1 and 0, then save up the result in a register.

My algorithm is as follows:

And this is the part of my code which is supposed do that:

    mov cx,0
    mov bl,8
input:
    mov ah,07h
    int 21h

    cmp al,46
    sub al,30h

    cmp al,0
    je valid
    cmp al,1
    je valid
    jmp input

valid:
    sub bl,1
    cmp bl,0
    je exit
    
    shl cl,1
    add cl,al
    jmp input

But when I check out the register cl using the emulator it never really shows me what I want; for example if I type 11000000 the value 60h ends up being saved in the cl register which obviously is not correct. What am I doing wrong?


Solution

  • if I type 11000000 the value 60h ends up being saved in the cl register which obviously is not correct.What am I doing wrong?

    The code is correct in asking for 8 valid binary digits. However, because of the particular emplacement of the instructions that decide about leaving the loop, the eighth digit is not stored in the CL register. You didn't allow shl cl,1 add cl,al to happen on that eighth digit.

    The quick fix is to change your code from:

    valid:
        sub bl,1
        cmp bl,0
        je exit       <<< exits too soon the 8th time
        
        shl cl,1      <<< these need to execute 
        add cl,al     <<<   the 8th time also
        jmp input
    

    to:

    valid:
        shl  cl, 1
        add  cl, al
        dec  bl
        jnz  input
    

    For a better/optimized version of this same loop, read my answer to your previous question Cannot understand why assembly isnt working output doesnt show correct result even tho value is copied correctly from the value register