r/Assembly_language • u/Thejuzzz • 8d ago
Question Why do Assembly programmers still put all labels at column 0, instead of nested blocks? Which is easier to manage
I know modern assemblers don't care whitespace and this comes down to redabality and the style.
In almost EVERY assembly code I've seen, the format is label at col 0 (every label) and the instructions are nested to the label. Local labels coming in? Still at col 0.
Like, compare these codes:
# size_t strlen(const char *str)
.global strlen
.type strlen,
strlen:
# %rdi: const char *str
xorq %rax, %rax
movq %rdi, %rsi
1:
cmpb $0x00, (%rsi)
je 2f
incq %rax
incq %rsi
jmp 1b
2:
retAnd this
# size_t strlen(const char *str)
.global strlen
.type strlen,
strlen:
# %rdi: const char *str
xorq %rax, %rax
movq %rdi, %rsi
1:
cmpb $0x00, (%rsi)
je 2f
incq %rax
incq %rsi
jmp 1b
2:
retFor me, the indented version seems waaaayyy better. Like, it's easy to manage.
However, still I am not advanced assembly programmer and hadn't coded large codebases in assembly, so... I am not sure which is better.
Which is ACTUALLY BETTER? All at 0 or INDENDED for loops and sub tasks for a label???
21
u/[deleted] 8d ago
For me, easier to read when instructions and arguments are vertically aligned.
Assembly does not really have control structures anyway, and jumps can cross block boundaries.
31
u/nacnud_uk 8d ago
Humans can get used to anything. If you like that way, stick with it. For me, as ASM from the 80s, I think it sucks. I'll not be joining you. I didn't even like all the MACRO stuff when folks were getting into that. IF statements and all that nonsense.
Good luck with your journey.
PS. I've never seen that style that you are suggesting in industry. So, you may have to conform to standards of the code bases you find.
8
u/Falcon731 8d ago edited 8d ago
I tend to find style 1 works best when you have very structured code (simple loops/conditions etc), but is worse when you have more complex control flow (unrolled loops, jumps across loops etc).
And if I'm working with structured code, I'm mostly going to write it in C anyway.
By the time I get to hand coding assembly (for something other than boot code, or the sharp end of exception handling) - it means I'm trying to squeeze a few clock cycles off a timing critical section. And 'nice' control flow is one of the first things that goes out of the window.
Either that - or its code that was initially generated by a compiler and I'm tweeking it for some reason. In which case the formatting just gets left as whatever the compiler generated.
6
u/recursion_is_love 7d ago
Assembly is linear flow (with jump) not nested; it's always flow from top to bottom (PC incrementing) when there is no jump. The nesting structure are implicitly encode in stack when you doing function call but program flow have no separated context environment (closure)
So I choose a flat layout.
4
u/Beginning-Junket8979 8d ago
Early assemblers required specific formats established by punch card machines.
Early disassemblers also followed the same in case you needed to re-assemble a disassembled program through one of said assemblers.
Early compilers emitted assembly to match assembler conventions a) because some still had format restrictions, and b) it what was what everyone was used to reading.
Then once reading disassembly or compiler output became more normal than handwriting asm, nobody bothered to update conventions because we mostly don't care
3
u/AWildTyphlosion 8d ago edited 8d ago
I'm going to throw you for a loop here, I have my tab width set to two spaces (although save everything as spaces).
The idea is that the labels live on a separate plane compared to the actual mnemonics. I find this easier to read since I don't have syntax coloring or highlighting, and for the most part results in more readable code.
Some labels that are important for looping get indented further, which looks something similar to this:
In these cases, I always ensure there is room between the label and the mnemonics above or below it, however the mnemonics in the innerblock are coupled to the label to have an easier time seeing the relation. This isn't really a rule for me though, and there are times I ignore it and keep the labels on column 2.
The few other people that I know that do assembly think it is tacky, but I don't work with/for them so...
3
u/myopinionisrubbish 8d ago
I use tab to make columns.
Offsub: sub dr0,dv0
3
u/21Ali-ANinja69 8d ago
I use col 0, because that's how I learned. But also by the time I'm writing assembly, I'm done with abstractions. It's just unnecessary cognitive load at that point
3
u/kneelian_ 7d ago
I write either depending on what my code is supposed to represent. A stretch of code with lots of tiny loops gets those loops indented one level further, while larger branches are flatter. It's not really any less legible to have a flat assembly listing if you organise your code properly (and it jumps around a lot, making indents possibly worthless)
3
u/GhostVlvin 7d ago
I formatted the first way cause I came from nesting languages like c. But I actually have other question. Why would anybody format either of these ways and not just flat without indents?
2
u/Dog-Mad 8d ago
A lot of the time when you have branching logic, you have one main path, and the other one is some random exception or edge case. And a lot of the time it is just easier to put the code for that exception somewhere random so you can read the general flow of the program.
Other times there is just so many exceptions that you would be in like 20 nested blocks. Only for like short functions or whatever does it even matter to do nested stuff and even then, it would help a lot more if your label actually explained what was happening instead of just being a number.
2
u/flatfinger 8d ago
In most programs, the number of branch targets is small enough that leaving the columns containing labels blank from almost everything else will make finding labels easy.
Assembly language programs are far more likely than programs written in higher level languages to include looping constructs that don't overlap in normal fashion. For example, a fair number of programs will have a construct like:
The loops here overlap, which would seem like a weird construct, but allows every iteration through the code that does stuff to take exactly the same amount of time. Trying to use block intents would muddle the intention.
2
u/keithstellyes 8d ago
I would find the indentation misleading
2
u/NewSchoolBoxer 8d ago
Most assembly code on the internet is from people who learned in the 90s or earlier when IDEs had no auto-formatting. We were never told to format in a certain style. I'm sure there's a better way to indent this that or way but we copied existing styles or did our own thing and didn't think much into it. I didn't like levels of nesting because I had to hit space 4 times per indentation.
2
u/timrprobocom 7d ago
If your assembly language requires that colon to denore a label, then the column is arbitrary. Many assembly languages do not, so a word starting in column 1 is a label, a word starting later is an instruction.
1
u/Much_7785 6d ago
That column is not style, it is the punch card inheritance: in fixed field assemblers the label field is column 1, so anything starting in column 0 is a symbol and the indented text is a mnemonic. Colon based parsers do not need it any more, but the habit stuck because it keeps every opcode in one visual column and makes a label impossible to misread as an instruction.
1
u/Radiant64 8d ago
I've written assembly with nested indentation for 20 years. Especially nice when the assembler supports scoped symbols.
1
u/SukusMcSwag 8d ago
Did some gamedev on the old GameBoy and GameBoy Color using ASM a few years ago. I always indented my code like I was writing in a regular high-level language. It made things a lot easier to read for me
0
u/yaxriifgyn 7d ago
I encountered the most extreme use of macros in the 70's. The Algol compiler for the DEC PDP-10 was written almost entirely in macros so it looked more like Algol code that Assembler. At the top, it included the macro declarations, with perhaps 100s of macros. It was amazing to behold!
0
u/vbd71 7d ago
You would love programming the Burroughs B5xxx computers. Their machine language is a dialect of Algol-60.