views:

57

answers:

2

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?

A: 

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.

Carl Norum
That would be nice, but my assembler is a beginners one (lc3) so that won't work. But I guess I can just use generic names myself 'while1' 'wend1' 'if1' 'ifend2' etc etc...
+6  A: 

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
Martin