assemblyembedded8051

Check if number is in range on 8051


I have received a character over UART, and need to validate if it's a number character.

Normally I'd do

if (char >= '0' && char <= '9') { /* VALID */ }

However, I have to do it in assembly.

I haven't found any compare instruction (so I assume there is none).

How can I do this?

    mov A, SBUF    ; load the number

    ; -- pseudocode --
    cmp A, #'0'    ; In AVR, I'd do it this way
    brlt fail      ; but I'm new to 8051
    cmp A, #'9'
    brge fail
    ; -- pseudocode --

    ; number is good

fail:

edit: ok here's what I have now but it doesn't work

;======================================================
; check if r1 is number char -> r0 = 1 or 0
;------------------------------------------------------
fn_isnum:
    PUSH acc
    PUSH 1

    MOV r1,A

    SUBB A,#'0'
    JC isnum_bad

    MOV A,r1

    SUBB A,#'9'+1
    JC isnum_bad

    POP 1
    POP acc

    MOV r0,#1
    RET 

isnum_bad:

    POP 1
    POP acc

    MOV r0,#0
    RET
;======================================================

Solution

  • The easiest thing to do is compile it in C and check the listing file. Here is what my compiler produces.

    MOV     A, SBUF
    CLR     C
    SUBB    A, #030H
    JC      ?C0026
    MOV     A, SBUF
    SETB    C
    SUBB    A, #039H
    JNC     ?C0026
    
    ; Put instructions here to execute when the code is valid
    
    ?C0026: