检查编号是否在8051的范围内 [英] Check if number is in range on 8051

查看:107
本文介绍了检查编号是否在8051的范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过UART收到了一个字符,需要验证它是否是数字字符.

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

通常我会做

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

不过,我必须组装.

我没有找到任何比较指令(所以我假设没有).

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

我该怎么做?

    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
;======================================================

推荐答案

最简单的方法是在C语言中对其进行编译并检查列表文件.这是我的编译器生成的.

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:

这篇关于检查编号是否在8051的范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆