Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running out of label names in assembly

Heyo,

My class at college has us writing programs in assembly. I have never truly appreciated the ease of C until now.

Now, when I program in assembly, I often have to make while/for/if loops and conditionals with labels eg:

SKIP:
    ...
COMP:ADD R1, R1, #0 ;Check for equality
     BRZ WHILEEND
     ...            ;code inside the while loop
     JMP COMP       ;Return to while loop
WHILEEND:
     ...

So, in this while loop (example) I have used 1 label for the subroutine and 2 more for the loop itself. I've run out of good label names for all the loops and branches I'm doing in assembly, what do you guys do to keep it varied and descriptive?

like image 221
user289293 Avatar asked Mar 09 '10 01:03

user289293


2 Answers

In a lot of assemblers you can make multiple labels with the same (usually numeric) name. That feature lets you reuse labels for your loops, using jmp 1f to jump forward to the nearest label 1 or jmp 1b to jump backward to the nearest label 1.

like image 182
Carl Norum Avatar answered Oct 03 '22 03:10

Carl Norum


Most assemblers allow local labels:

routine_1:
  ...
.loop:
  ...
  jne .loop

routine_2:
  ...
.loop:
  ...
  jne .loop
  ...
  jmp routine_1.loop

or anonymous labels where you can reuse the same label name and reference "closest backward" or "closest forward":

routine_1:
  ...
@@:
  ...
  jne @b

routine_2:
  ...
@@:
  ...
  jne @b

(b for backwards)

If neither is supported in your assembler, I suppose you could prefix all local labels with the label of the routine in question:

routine_1:
  ...
routine_1.loop:
  ...
  jne routine_1.loop
like image 26
Martin Avatar answered Oct 03 '22 02:10

Martin