I'm wondering, Does assembler support if staments? (like a C program) Tried something very simple:
org 0h sjmp BEGIN org 50h BEGIN: IF R0 == #45 MOV A, #54 CPL A ELSE MOV A, #45 CPL A ENDIF END
Does assembler support if staments Yes, but not in the way that you mean. There are several conditional statements you can use in assembly, but the can only check the values of SET, EQU, or other fixed expressions. They cannot check things that change at runtime. Refer to: http://www.keil.com/support/man/docs/a51/a51_ap_conditional.htm Jon
This simple example it's just a #ifdef for set/equ. That's too bad, C-like IF statament would make life much easier. Thank you both for reply.
C-like IF statament would make life much easier Get "the bible" and for this read chapter 2 Erik here are the links to "the bible" Chapter 1 http://www.semiconductors.philips.com/acrobat/various/80C51_FAM_ARCH_1.pdf chapter 2 http://www.semiconductors.philips.com/acrobat/various/80C51_FAM_PROG_GUIDE_1.pdf chapter 3 http://www.semiconductors.philips.com/acrobat/various/80C51_FAM_HARDWARE_1.pdf
Sergio, Yes, you can implement assembly IF statements using ASM macros, but they won't read like higher level if statements. Still the same, such macros for ASM51 are very useful and, if you code a lot of assembly, I'd recommend creating a "language extension" file that contains all of your useful a51 macros. Following is an example of just a few test/jump macros. Note, there's nothing magical about these; they simply consolidate some of the more commonly used opcode sequences. There's many other categories of macros that can be considered as well...namely DPTR related code sequences.
; IF x < y THEN GOTO addr ; jl MACRO x, y, addr clr c mov a, x subb a, y jc addr ENDM ; IF x <= y THEN GOTO addr ; jle MACRO x, y, addr clr c mov a, y subb a, x jnc addr ENDM ; IF x > y THEN GOTO addr ; jg MACRO x, y, addr clr c mov a, y subb a, x jc addr ENDM ; IF x >= y THEN GOTO addr ; jge MACRO x, y, addr clr c mov a, x subb a, y jnc addr ENDM jne MACRO x, y, addr mov a, x xrl a, y jnz addr ENDM je MACRO x, y, addr mov a, x xrl a, y jz addr ENDM
; If R0 <= B, then jump to MyJumpAddr ; jle R0, B, MyJumpAddr
That's too bad, C-like IF statament would make life much easier. Indeed. That's why there's a C-like if statement --- in C. If C is what you want, use it. If C and assembly were the same thing, as your wish essentially implies, one of the two would be superfluous.
jl MACRO x, y, addr [...]
A hint regarding all those macros: cjne is better for most of these than subb or xrl. In particular, you can easily get three exits from a single macro: one for a < b, one for a == b, and one for a > b. And the content of the accu at exit from the macro will be one of its operands, rather than their difference. Obviously, but CJNE doesn't accept just any operands. The macros as they're written, on the other hand, will accept any Rn, #data, or direct in either x or y parameter. True, they may not be as efficient as possible, but the couple of extra machine cycles is a small price to pay to have intuitive instructions that work for any type of operands.
Thank you Robert, that helped me a lot.
View all questions in Keil forum