Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Assigning a value to a variable in Assembly.

Status
Not open for further replies.

Peet19

Member
Hi everyone!
I would like to ask how to pass the value of a register to a variable in Assembly?
I get the value of ADRESH, which we enter in W.
EVAL MACRO
LOCAL RH
MOVF ADRESH, W
;...;
ENDM
How should the value of this be transferred to the RH variable?

Thank you in advance for your help!
 
It would also be a good solution if I could pass the values of the ADRESH and ADRESL registers in the parameters of the macro call.

EVAL ADRESH, ADRESL ; Macro calling

Unfortunately, this is not good.
 
Unfortunately it is not good.
I would also be happy if I could pass the value of the register to the variables of the macro call.
That would be the best solution.

RESH EQU 0X20
RESL EQU 0x21

MOVLW 0
MOVWF RESH
MOVLW 143
MOVWF RESL

EVAL RESH, RESL ; Macro calling
 
I am guessing that you are using a PIC microcontroller and that ADRSEL and ADRSH are the low aand high bytes of the value read fro an analog to digital convrerter.
You will first have to assign physical memory locations to store the variable RH.
As variable RH is a 16 bit value you will need to assign two 8 bit memory locations as RH_L and RH_H .(You don't have to use _L and _H you any use anything to distinguish the high and low bytes.

Here is an example
Code:
; Bank 0
        cblock  RAM_START

param1:    1          ; parameter 1    (Used in delay cycles routine)
param2:    1          ; parameter 2    (Used in delay cycles routine)
Temp_1:    1        ;Used in 2 second delay 

tmpData:    1        ; Used in output_hexbyte routine

; delay counters
Del_Count:     1
        

BME_Rdata:    1        ;Date read from BME280 register
BME_Wdata:    1        ;Date written to BME280 register

Raw_Data0:    1        ; Most significant
Raw_Data1:    1    ; 
Raw_Data2:    1    ; 
Raw_Data3:    1    ;
Raw_Data4:    1    ; Least significant
In the above example Raw_Data is a 40 bit value so it has to use 5 8 bit (Byte) memory locations.
All the required variables have to be assigned memory locations at the start of the program.

I hope this answeres your question.

Les.
 
Unfortunately, it's not good either. I am copying the entire code.
Here is the macro:
EVAL MACRO RH
LOCAL A
A = RH * 25
MOVLW A
MOVWF BIGH
ENDM

Here is the macro call:
MOVLW 5
MOVWF RESH
EVAL RESH ; Macro calling

I simplified it a bit, for the sake of testing.
I want the value of the RESH register to be in the argument of the macro call, but it doesn't work at all.
In the macro's argument, it will not be the value of RESH, but its address.
I'm going crazy.
Please help me how to solve this?
 
The problem is that the address of the register is passed to the macro (and it calculates incorrectly) and not its value.
Is it possible to pass a register value to a macro anyway?
Is there such a possibility at all?
 
Yes... All pics have a "pointer" called the FSR and that can hold the address of any register.

You do realize that ALL of the registers in a pic are accessed as file.. movff f,f is register to register. ( as long as they are a 12 bit address apart..).

Pic 18's have tables as well Ideal for large data chunks. ie. 32 bit containers or more.
Table read / writes have auto increment / decrement post and pre instruction...

Macro using a "local" variable may not work for you, as I have always thought that local variables are destroyed once the macro ends..( I may have this wrong ) Nigel would be the best to direct you here .
 
I think you misunderstand how MPASM works.
EQU doesn't define a variable, it just creates an assembler symbol
Likewise, in the code below 'A' is not a variable accessible to the micro, it's an assembler variable.
The statement 'A = RH * 25' doesn't generate any executable asm code, again it's just an assembler statement.
And, a macro is not a subroutine call and asm isn't a high-level language.

Code:
EVAL MACRO RH
   LOCAL  A
   A = RH * 25
   MOVLW    A
   MOVWF    BIGH
ENDM

Here's an example that works (somewhat). Multiplying a byte register by 25 can overflow a byte destination, so you've been warned.
Code:
  processor 18F26K22
  include p18f26k22.inc   ; for PRODH, PRODL
 
RESH EQU 0x20
RESL EQU 0x21
BIGH EQU 0x22

; multiply the register 'x' by 25 and put the low byte of the result in reg BIGH
; 'x' and 'BIGH' can be located in any bank
EVAL MACRO x
  MOVFF x, WREG     ; mov reg 'x' to W for mul
  MULLW 25          ; 16-bit result is in PRODH:PRODL
  MOVFF PRODL, BIGH ; throw away the high byte
  ENDM

  MOVLW 5
  MOVWF RESH
  EVAL RESH

  END

When the macro gets expanded it produces:
Code:
000000 0E05           00016   MOVLW 5
000002 6E20           00017   MOVWF RESH
                      00018   EVAL RESH
000004 C020 FFE8          M   MOVFF RESH, WREG  ; mov reg 'x' to W for mul
000008 0D25               M   MULLW 25          ; 16-bit result is in PRODH:PRODL
00000A CFF3 F022          M   MOVFF PRODL, BIGH ; throw away the high byte
 

Ian Rogers​

Thanks!
I realized that it is not possible to pass a variable value to the macro while the program is running, because the macro is a translation control directive and after translation, the finished complete code is already in the program memory. With fixed values. So you can't actually work with changing values "on the fly" in the macro.
Thanks for the links too.

tumbleweed​

Thanks, you too. What you wrote is completely understandable and the code works fine, but it would have been important for me to put the value passed to the macro in a variable so that I could perform further mathematical operations with it. But you're right, it's not a high-level language, so I have to approach it differently. Thanks again.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top