Load external symbol into register

Load external symbol into register

Postby ralisi » Fri Apr 11, 2014 12:11 pm

I am trying to jump to a C-function using the JR command. The reference manual states the following:

Example:
Code: Select all
MOV R0,#_labA ;move label into register
JR R0; ;jump to _labA


This is what I want to do, but it does not work, as "#" gets expectedly treated as a comment, as stated in the sdk manual.
Anything to the right of the character ‘#’ is a comment


For now, I am only trying to load the adress of a label into a register but it does not work.

My current code for testing is:
Code: Select all
startit:
nop
b foo
nop
mov r0, foo
mov r1, #foo
mov r3, %high(foo)
mov r4, %low(high)
nop
nop



foo:
nop
nop


But the assembler always produces the following code:
Code: Select all
Disassembly of section .text:

00000000 <startit>:
   0:   01a2            nop
   2:   0ce0            b 1a <foo>
   4:   01a2            nop
   6:   000b 0002       mov r0,0x0
   a:   200b 0002       mov r1,0x0
   e:   600b 0002       mov r3,0x0
  12:   800b 0002       mov r4,0x0
  16:   01a2            nop
  18:   01a2            nop

0000001a <foo>:
  1a:   01a2            nop
  1c:   01a2    nop


What is the syntax to load the adress of a label?
ralisi
 
Posts: 15
Joined: Fri Apr 11, 2014 12:00 pm

Re: Load external symbol into register

Postby timpart » Fri Apr 11, 2014 1:21 pm

ralisi wrote:I am trying to jump to a C-function using the JR command. The reference manual states the following:

Example:
Code: Select all
MOV R0,#_labA ;move label into register
JR R0; ;jump to _labA


You want
Code: Select all
MOV R0,%low(_labA) ;move bottom half of label into register
MOVT R0,%high(_labA) ;move top half of label into top half of register
JR R0; ;jump to _labA


The MOVT instruction could be omitted if you are absolutely certain the address will be in the first 64K of memory. If you aren't sure, keep it in.

The assembler does put zeros in the place where the address would go. These zeroes are resolved by the linker when it works out where the address actually is.

I think the example you given from the manual was referring to using the assembler which does accept # before a number. The example does assume the address is in local memory!

Tim
timpart
 
Posts: 302
Joined: Mon Dec 17, 2012 3:25 am
Location: UK

Re: Load external symbol into register

Postby ralisi » Fri Apr 11, 2014 4:50 pm

Thank you for your answer. I forgot to use the linker, as I was working with only one file at that time.
ralisi
 
Posts: 15
Joined: Fri Apr 11, 2014 12:00 pm

Re: Load external symbol into register

Postby notzed » Sat Apr 12, 2014 11:47 pm

fwiw the # is optional for constants. But I tried removing them and it makes the code difficult to read - it's too easy to mistake small constants for registers so I'm making a habit of keeping them in myself.
notzed
 
Posts: 331
Joined: Mon Dec 17, 2012 12:28 am
Location: Australia

Re: Load external symbol into register

Postby timpart » Wed Apr 16, 2014 6:26 am

notzed wrote:fwiw the # is optional for constants. But I tried removing them and it makes the code difficult to read - it's too easy to mistake small constants for registers so I'm making a habit of keeping them in myself.


Yes I always use # for constants too. I use e-as to assemble my code. I'm just wondering if @ralisi was embedding assembly within a C program and the precompiler was seeing the # and trying to use it? I've never tried assembler within C myself.

Tim
timpart
 
Posts: 302
Joined: Mon Dec 17, 2012 3:25 am
Location: UK

Re: Load external symbol into register

Postby notzed » Wed Apr 16, 2014 12:49 pm

timpart wrote:
notzed wrote:fwiw the # is optional for constants. But I tried removing them and it makes the code difficult to read - it's too easy to mistake small constants for registers so I'm making a habit of keeping them in myself.


Yes I always use # for constants too. I use e-as to assemble my code. I'm just wondering if @ralisi was embedding assembly within a C program and the precompiler was seeing the # and trying to use it? I've never tried assembler within C myself.

Tim


If you end a file in .S it goes through the c pre-processor but quoted strings in asm() wont since it's spliced in well after that has run. The gcc asm output (-S) includes # in it's output for constants, fwiw.

I think the manual is just incorrect. Lines that don't contain instructions handle # has a comment starter, but if there is any instruction there it breaks:

a.s =
# comment 0
label: # comment 1
mov r0,1 # comment 2
rts # comment 3

$ e-gcc -c a.s
a.s: Assembler messages:
a.s:4: Error: unrecognized keyword/register name `mov r0,1#comment 2'
a.s:5: Error: unrecognized form of instruction `rts #comment 3'
$ e-as -c a.s
a.s: Assembler messages:
a.s:4: Error: unrecognized keyword/register name `mov r0,1#comment 2'
a.s:5: Error: unrecognized form of instruction `rts #comment 3'

Actually:

mov rx,#label
movt rx,#label
(with or without the hash)

seems to be the equivalent of:

mov rx,%low(label)
movt rx,%high(label)


PS back to the original question using "e-objdump -dr" on unlinked objects will include any reloc info in the disassembly so you can see which 'empty values' will actually be replaced by the linker and by what relocation method.

The example:

Code: Select all
Disassembly of section .text:

00000000 <startit>:
   0:   01a2            nop
   2:   00e8 0000       b 2 <startit+0x2>
                        2: R_EPIPHANY_SIMM24    foo
   6:   01a2            nop
   8:   000b 0002       mov r0,0x0
                        8: R_EPIPHANY_LOW       foo
   c:   200b 0002       mov r1,0x0
                        c: R_EPIPHANY_LOW       foo
  10:   600b 0002       mov r3,0x0
                        10: R_EPIPHANY_LOW      foo
  14:   800b 0002       mov r4,0x0
                        14: R_EPIPHANY_LOW      high
  18:   01a2            nop
  1a:   01a2    nop



Interesting that %high() isn't actually high for a mov. I also checked %low() equally becomes a %high() for a movt. Doesn't seem much point using them?
notzed
 
Posts: 331
Joined: Mon Dec 17, 2012 12:28 am
Location: Australia


Return to Assembly

Who is online

Users browsing this forum: No registered users and 1 guest

cron