Search code examples
assemblymasmirvine32

how to differentiate between when i press enter and write 0 in Assembly?


Include Irvine32.inc

.data
prompt1 BYTE "Bye!", 0
prompt2 BYTE "Type an integer : ", 0

.code
MAIN PROC

    mov edx, OFFSET prompt2
    call WriteString

    call ReadInt

    exit
MAIN ENDP

end main

I would like to end the program when I just press enter key, And print value when I write integer value(-2^15 ~ 2^15-1).

I have a problem that I don't know how to let compiler differentiate when I write 0 value and press enter key. All of flags and registers value are same when I write 0 and press enter key. so I can't differentiate it on the code.

I could solve this problem when I call WriteChar and check whether first character is enter or not, but it was very complex code. When I wrote value in case calling WriteChar, I had to change value from string to integer.

Is there any easy way to solve this problem?


Solution

  • No way! You have to rewrite Irvine's ReadInt procedure.

    The original function uses ReadString and stores the result in ECX. However, due to USES ecx edx the former value of ECX will be restored at the end of the procedure. Just to change it to USES edx solves the problem. ECX contains now the size of the inputted string.

    Include Irvine32.inc
    
    .data
    prompt1 BYTE "Bye!", 0
    prompt2 BYTE "Type an integer : ", 0
    promptBad BYTE "Invalid input",0
    
    .code
    MAIN PROC
    
        mov edx, OFFSET prompt2
        call WriteString
    
        read:
        call myReadInt
    
        jo  error
        jecxz error
    
        call WriteInt
        exit
    
    error:
        mov  edx,OFFSET promptBad
        call WriteString
        exit
    MAIN ENDP
    
    myReadInt PROC USES edx
    LOCAL digitBuffer[50]:BYTE
    
        lea   edx, digitBuffer
        mov   ecx,50
        call  ReadString
        mov   ecx,eax   ; save length in ECX
    
        call    ParseInteger32  ; returns EAX, CF
    
        ret
    myReadInt ENDP
    
    end main