variable declration in 8051 assembly

Hi! Can anyone tell me how to declare 8-bit or 16-bit variables while programming in assembly language for 8051 microcontroller (AT89C51), using the on-chip RAM.

Reply to
Ammar
Loading thread data ...

Can't be done, as assembly language doesn't really have the concept of a "variable", much less that of a 16-bit variable on an 8-bit microprocessor.

In other words: if you have to ask, you shouldn't be writing assembly code yet. Stick with C for the time being.

Reply to
Hans-Bernhard Bröker

For the on-chip RAM I use EQU (equates) to define locations in direct and indirect space. Granted, addresses must be assigned by hand, but given the typical small number of locations it isn't a problem.

For example: X EQU 60h MOV A,X ADD A,#4 MOV X,A

Syntax may vary by assembler. Jack Peacock

Reply to
Jack Peacock

Nonsense. If you understand the underlying machine you can do anything in assembly that you can do in C, and more. However, it won't be portable.

To answer the OP, there are usually pseudo instructions such as DS (define storage) or DW (define word, i.e. initialized storage) or DB (define byte, again initialized storage). Placement of these instructions is likely to be critical. Read your assembler documentation. Read the manuals for your processor. The mnemnonics may not agree.

--
Merry Christmas, Happy Hanukah, Happy New Year
        Joyeux Noel, Bonne Annee.
 Click to see the full signature
Reply to
CBFalconer

Then you're making your life more miserable than hit has to be. All

8051 assemblers worth using have a DB pseudo-op that lets the assembler/linker take care of locations and their usage for you.
Reply to
Hans-Bernhard Bröker

While that's correct, it's not what I was talking about. Assembly doesn't have variables, it only has symbols, i.e. named addresses. That doesn't prevent a program written in assembly from using such concepts as "variables" --- but assembly language as such doesn't support them in any good sense of the word "support".

The crucial aspect missing is that assembler symbols don't really have a type. Neither the 8051 nor the assembler care whether a given allocation of 4 bytes of data memory ("DS 4" in most 8051 assemblers) holds 32 individual bits, 4 bytes, 2 16-bit numbers a 3-byte generic pointer plus a flag byte, or something entirely different. That's all up to the programmer.

A "DS 2" can be used as a part of the implementation of a 16-bit variable, but that doesn't mean it _is_ a 16-bit variable.

Reply to
Hans-Bernhard Bröker

The free SesAsm51 assembler implements types and sizes. You may declare variables of any byte sizes in any of the 8051 memory spaces.

Here's a download address:

formatting link

Happy New Year!

Reply to
Schjaer

I use it as a way to track the different namespaces for RAM memory. and DS for external RAM, since there may be a large number of variables, and EQU for direct since it is a limited resource and I like to track each byte allocated. It's a habit from the days of 8048 assembler.

On the other hand, with 8051's now having so much onboard flash and RAM, I try to do everything in C (SDCC-51 compiler) rather than assembler. Jack Peacock

Reply to
Jack Peacock

We're splitting the hairs quite finely, aren't we? It is granted that ASMs don't know anything about data types, sizes, allocation, etc., but ASMs provide the mechanisms for the programmer to declare and access "variables".

Reply to
Everett M. Greene

These named addresses can have various attributes (at least in relocating assemblers), some of which are inherited from the program section in which the address is defined, such as program section name, absolute/relocatable, RW/RO etc. It would not be too far to extend this with byte/word/long attributes generated by BYTE/WORD/LONG storage initialisation directives.

Quite a few assemblers can select between short and long branches based on the information of the program section and offsets within sections. Some assemblers might be able to automatically generate relative/absolute addressing modes depending of the attributes of the target label. If the label carries some kind of byte/word information, it would be possible for the programmer to use a generic MOV mnemonic instead of explicit MOV_B/MOV_W opcode mnemonics.

What actually is a variable with a type ? Is it something radically different from a symbol with attributes ?

DS (Define Storage) is analogous to the memory allocation made available by C malloc function, both representing a chunk of bytes, although the malloc allocation requires an extra indirection to access. You can do anything with the mallocated storage as you can do with DS storage, as long as you stay within the allocation (for processors with specific alignment rules for longer data types, the allocation start must be located according to the alignment rules).

Paul

Reply to
Paul Keinanen

That's right, I am new in assembly language programming but somehow I have to do it. What I wanted to ask was the syntax of using a variable (i.e. an 8 bit location) Should this be declared in separate 'data' segment?

Reply to
Ammar

Incomprehensible without quotes. What's right?

Don't top-post. Do quote. Articles need to stand by themselves on usenet. See the following links:

--
 Some informative links:
     (newusers)
 Click to see the full signature
Reply to
CBFalconer

As mentioned in several of the responses, most assemblers will allow you to declare variables of several types, but the programmer must insure that the variables are used correctly.

A variable is generated at assembly time by reserving storage at an address (Starting address of first byte, or bit) and may initialize it with a value(s), or simply reserve space.

I used the metalink assembler to create an expansion to BASIC-52 and created constants and tables required in my program, as well as the tables, flags, and vector addresses required by the interpreter at the addresses specified in the MCS-51 BASIC Manual.

The assembler and the manuals are available online, and the manual has many more examples of code and calling conventions. Probably more than you want to know!

The original program was developed on a BCC-52 with a ROMA+B and 32KB of RAM. (ROMA+B was itself an 8KB expansion ROM, and contained the ASM, editor, and many of the same type commands I was adding. The difference is that ROMA+B cost $150+) The RAM was used by the assembler as working storage, symbol tables, and the resulting program, which didn't allow many labels. The part below was after I bought a PC and could use the Metalink assembler!

My expansion was developed to provide a way to test assembler code from within the BASIC interpreter, with a PC or a small terminal as the console, which required several versions of the display output.

Below are several types of "variables" and constants in various formats from the expansion ROM I wrote. The MAKE_TOKEN macro is defined below the code examples, and shows how to generate a basic "structure" in this assembler. You will need to see the BASIC manual to make sense of the values and formats...

Enjoy.

Gary

========Code snipped from ROM expansion========= $INCLUDE(MACRO.SRC) ; add new instruction macros ; VARIABLES WHICH CONTROL THE FORMAT AND CONTENT OF THE EXPANSION ; ROM. ; SMALL_SCRN EQU 0 ; SHORTEN MOST DISPLAYS TO FIT ON 40 CHAR SCREENS DEC_HDR EQU 0 ; MODIFIES THE DISPLAY WHEN USING THE DIS.COMMAND ; SET TO 0 TO REMOVE THE DECIMAL ADDRESS COLUMN AND ; EXPAND THE NUMBER OF DISPLAYED CHARACTERS/LINE ; ; GENERAL EQUATES FOR READABILITY ; CR EQU 0DH ; C RETURN LF EQU 0AH ; LINE FEED ROM_EOM EQU 22H ; END OF DISPLAY TEXT IN ROM ; ; SELECTED OPBYTEs to BASIC ; D_STR EQU 6 ; DISPLAY STRING (R3:R1=POINTER) R_MSG EQU 52 ; FLAG BIT FOR ROM MSG G_STR EQU 5 ; INPUT STRING (INTO BASIC LINE BUFFER) D_CHR EQU 80H ; DISPLAY CHAR IN R5 D_HEX EQU 98H ; DISPLAY HEX N_L0 EQU 36H ; BIT WHEN SET-SUPPRESS LEADING 0'S FOR D_HEX L_CHR EQU 3FH ; LOOK AT NEXT CHAR IN BASIC BUFFER G_CHR EQU 40H ; GET NEXT CHAR IN BASIC BUFFER & ADV POINTER G_CON EQU 41H ; GET CHAR FROM CONSOLE PUT IN ACC E_ARG EQU 39H ; EVALUATE EXP (BASIC BUFFER),PUSH ON ARG STACK G_ARG EQU 1 ; POP (16BIT) VALUE FROM ARG STACK TO R3:R1 D_NL EQU 7 ; NEW LINE-SEND CR/LF TO CONSOLE

ORG 2000H ; EXTENSION STARTS AT 2000H DB 00H DB 00H DB 5AH ; FLAG EXPANSION OPTION PRESENT ORG 2048H ; CALLED BY BASIC WHEN INITIALIZING SETB 45 ; FLAG EXPANSION HERE-FOR BASIC JB 21H.7,NOT_COLD_START ; FIRST TIME? SETB 21H.7 ; TO KEEP THE (c) FROM COMING UP AGAIN AJMP C_WRITE ; PRINT THE COPYWRITE THIS TIME NOT_COLD_START: RET ORG 2070H MOV DPTR,#VECTOR_TABLE ; TELL BASIC WHERE JUMP TABLE IS RET ; AND RETURN ORG 2078H MOV DPTR,#USER_TABLE ; USER TOKEN TABLE RET ; VECTOR_TABLE: DW DO_DISPLAY ; ADDRESSES MUST BE IN THE SAME ORDER DW HEX_L ; AS THE TOKENS! DW BURN_EPROM ; BASIC USES " (TOKEN-10H)*2 " DW MOV_MEM ; AS AN INDEX INTO THIS TABLE, DW HELP ; WHICH MEANS YOU SHOULD NOT HAVE DW FIL_BLK ; GAPS BETWEEN TOKEN #'S DW VER_MEM DW HEX_ERR ; GO TO AN EXIT CALL FOR DUMMY COMMAND ORG VECTOR_TABLE+(2*17) ; RESERVE SPACE FOR MAX TOKENS ; TOKEN_NUM SET 0FH ; BIAS FOR MAKE_TOKEN MACRO USER_TABLE: MAKE_TOKEN 'DIS.' ; DISPLAY CODE/DATA/EXTERNAL MEMORY MAKE_TOKEN 'HEX.' ; LOAD/SEND INTEL HEXFILE MAKE_TOKEN 'PRG.' ; PROGRAM AN EPROM/RAM MAKE_TOKEN 'MOV.' ; MOVE BLOCKS OF MEMORY AROUND (SMART) MAKE_TOKEN 'HELP' ; HELP COMMAND FOR EXTENSIONS MAKE_TOKEN 'FIL.' ; FILL RAM WITH A PATTERN MAKE_TOKEN 'VER.' ; VERIFY A MOVE/PRG WORKED ; ; ***** ADD NEW TOKENS HERE ***** ; LAST_TOK EQU TOKEN_NUM ; FOR USE BY HELP COMMAND/DUMMY TOKEN DB LAST_TOK+1 ; WASTE A TOKEN BECAUSE OF ERRORS IN BASIC DB 1FH,'dumy' ; DUMMY COMMAND, SEE ROMJ USER MANUAL! DB 0FFH ; FLAG END OF USER EXPANSION ; ; ORG 20E8H ; ALLOW EXPANSION OF TOKENS HEX_L: ; LOAD AN INTEL HEX FILE LJNB 47,DUMMY ; COMMAND MODE? MOV 21H,#80H ; CLR FLAGS EXCEPT THE (C) ACALL PEEK ; CHECK FOR SEND (L_CHR) CJNE A,#'A',$+3 ; NUMBER? JC HEX_N1 ; COULD BE CLR ACC.5 ; MAKE UPPER CASE CJNE A,#'L',$+7 ; LOAD? ACALL GCI ; YES, WASTE CHAR (G_CHR) . . ==============end of ROM snipped========================== MACRO.SRC $NOLIST ; I am very lazy when it comes to rewrites of instructions after crossing ; page boundries. Each time the program I was working on became large ; enough to generate relative addressing errors, I only ; had to change the first character to correct the problem. ; example: SJMP => AJMP or AJMP => LJMP ; AJNB MACRO BNUM,ADDR ;;CONVERT SJMP TO PAGE JUMP JB BNUM,$+5 AJMP ADDR ENDM ; LJNB MACRO BNUM,ADDR ;;CONVERT PAGE JUMP TO LONG JUMP JB BNUM,$+6 ;;THIS DESTROYS RELOCATION FEATURE! LJMP ADDR ENDM ; AJB MACRO BNUM,ADDR ;;ALLOW JB 2K ACCESS JNB BNUM,$+5 AJMP ADDR ENDM ; LJB MACRO BNUM,ADDR ;;ALLOW JB 64K ACCESS JNB BNUM,$+6 ;;DESTROYS RELOCATION FEATURE LJMP ADDR ENDM ; TJE MACRO REG,VALUE,ADDR ;;SJMP ON EQUAL CJNE REG,VALUE,$+5 SJMP ADDR ; EQUAL ENDM ; ATJE MACRO REG,VALUE,ADDR ;;AJMP ON EQUAL CJNE REG,VALUE,$+5 AJMP ADDR ; EQUAL ENDM ; ACJNE MACRO REG,VALUE,ADDR ;;ALLOW CJNE 2K ACCESS CJNE REG,VALUE,$+5 SJMP $+4 ; EQUAL AJMP ADDR ; NOT = ENDM ; TLE MACRO REG,VALUE,ADDR ;;MACRO DEF TO MAKE JUMP LESS THAN OR = CJNE REG,VALUE+1,$+3 JC ADDR ; REG < OR = VALUE ENDM ; ATLE MACRO REG,VALUE,ADDR ;;MACRO DEF JUMP < OR = CJNE REG,VALUE+1,$+3 JNC $+4 AJMP ADDR ; REG < OR = VALUE ENDM ; SCASE MACRO REG,VALUE,LESS,EQUAL,GREATER ;; ALL TESTS AT ONCE CJNE REG,VALUE,$+3 JC LESS ; REG < VALUE CJNE REG,VALUE+1,$+3 JC EQUAL ; REG = VALUE SJMP GREATER ; REG > VALUE ENDM ; ACASE MACRO REG,VALUE,LESS,EQUAL,GREATER ;; ALL TESTS AT ONCE CJNE REG,VALUE,$+3 ;; 2K RELOCATION JNC $+4 AJMP LESS ; REG < VALUE CJNE REG,VALUE+1,$+3 JC $+4 AJMP GREATER ; REG > VALUE AJMP EQUAL ; REG = VALUE ENDM ; LCASE MACRO REG,VALUE,LESS,EQUAL,GREATER ;; ALL TESTS AT ONCE CJNE REG,VALUE,$+3 ;; NO RELOCATION JNC $+4 LJMP LESS ; REG < VALUE CJNE REG,VALUE+1,$+3 JC $+4 LJMP GREATER ; REG > VALUE LJMP EQUAL ; REG = VALUE ENDM ; ;THE FOLLOWING LINE MUST BE ADDED TO THE BEGINNING OF THE EXPANSION PROG! ;TOKEN_NUM SET 0FH ; USER TOKENS START AT 10H ; MAKE_TOKEN MACRO TOK_TXT ;; FORMAT BASIC-52 USER TOKEN TOKEN_NUM SET TOKEN_NUM+1 ;; NEXT TOKEN # TO USE DB TOKEN_NUM ;; THE TOKEN # FOR DB TOK_TXT ;; TOK_TXT USER-TOKEN DB 00H ;; END OF TOKEN ENDM $LIST

Reply to
gary drummond

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.