flat assembler
Message board for the users of flat assembler.

Index > Main > Store address of label in another variable

Author
Thread Post new topic Reply to topic
nisargshah95



Joined: 23 Jun 2016
Posts: 7
Location: India
nisargshah95 23 Jun 2016, 17:40
I am new to FASM (ver 1.71.16) and trying to write a program which has the following simplified structure -

Code:
start:
    ;; some condition using cmp
    je @f
    mov [block], block1
    jmp .forward
  @@: 
    mov [block], block2
  .forward:
    ;; go to block.sub1

block1:
  .sub1:
  .sub2:

block2:
  .sub1:
  .sub2:

;; data
block rd 1    


I wish to store the address of block1 or block2 (depending on a condition into block variable and then jump to .sub1 sub label. How do I do this?
Post 23 Jun 2016, 17:40
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20453
Location: In your JS exploiting you and your system
revolution 23 Jun 2016, 23:56
Code:
mov [block],block1.sub1
jmp [block]    
Post 23 Jun 2016, 23:56
View user's profile Send private message Visit poster's website Reply with quote
nisargshah95



Joined: 23 Jun 2016
Posts: 7
Location: India
nisargshah95 24 Jun 2016, 04:14
revolution wrote:
Code:
mov [block],block1.sub1
jmp [block]    

I know I can do that, but I was looking for a way such that I would not have to move local label's address into register every time I want to access it.
Post 24 Jun 2016, 04:14
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20453
Location: In your JS exploiting you and your system
revolution 24 Jun 2016, 05:59
I don't understand what you mean. There are no registers used in any code posted in this thread so far.
Post 24 Jun 2016, 05:59
View user's profile Send private message Visit poster's website Reply with quote
nisargshah95



Joined: 23 Jun 2016
Posts: 7
Location: India
nisargshah95 24 Jun 2016, 06:19
Sorry I did not make it clear. Throughout the program, I will only be executing the code in either block1 or block2 (both have the same local label names) based on a condition (ie, one of them will not be used during the entire execution). So throughout the program instead of checking every time whether block1 is to be used or block2 I simply want to call the local labels using block.labelname
Post 24 Jun 2016, 06:19
View user's profile Send private message Reply with quote
nisargshah95



Joined: 23 Jun 2016
Posts: 7
Location: India
nisargshah95 24 Jun 2016, 06:22
Based on a discussion with a friend, I was given the following suggestion. Keep the address of the local labels at the start of block1/block2, And access the code at that local label this way
Code:
start:
    ;; some condition using cmp
    je @f
    mov [block], block1
    jmp .forward
  @@: 
    mov [block], block2
  .forward:
    jmp [block+sub1]

block1:
  dd .sub1
  dd .sub2

  .sub1:
    ;; code
  .sub2:
    ;; code
    
Post 24 Jun 2016, 06:22
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 24 Jun 2016, 11:23
Why don't you use a direct call rather than moving into a variable ?

Code:
jmp block1.sub1
or even faster
jpe block2.sub1
    
Post 24 Jun 2016, 11:23
View user's profile Send private message Send e-mail Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2568
Furs 01 Jul 2016, 23:29
You can't jump to the respective block's sub by just storing one value. Jump instructions store offsets to the respective location (either relative, if direct jump, or absolute, if indirect).

Since I assume block1 and block2 are different pieces of code, then their subs are definitely different code, thus different sizes. You can't do, for example, block+x, where x is block1.sub1-block1, because then it would fail on block2.sub1, assuming it has a different offset (relative to block2) which is probably the case...?

However, you can store an index to a table, or simply offset to the table as you did in your second attempt, the table holding offsets for the sub1 and sub2 for each block etc.

Your discussion with a friend as you mentioned it does indeed use that method, which works fine. Any problems with it or did you just post because you found the solution? Smile
Post 01 Jul 2016, 23:29
View user's profile Send private message Reply with quote
nisargshah95



Joined: 23 Jun 2016
Posts: 7
Location: India
nisargshah95 09 Jul 2016, 04:11
@Furs I posted it because I wanted an opinion on how effective a solution it is, and whether a better approach exists. Nevertheless, I used that approach in the end.
Post 09 Jul 2016, 04:11
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2568
Furs 13 Jul 2016, 16:47
Don't worry about it, it is the best solution if you have many such blocks or many conditions, that's how you can make "objects" in C or asm (which don't have abstract OOP things), because basically those offsets are "function pointers" (if you know C).

Of course in asm you have more control and they don't have to be functions (you use jmp instead), as long as they all have same entry conditions it should work fine. Smile
Post 13 Jul 2016, 16:47
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.