Question

I am taking an assembly class, and the exams are coming, so I have to ask, what's the standard format and structure of the source code in a non-structural language like assembly (8086 specific)? Currently I like to format the code like below, having everything that executes after a label in a new tab column and not using unnecessary labels that I will not use later.

code segment
    start:  
        mov ax,data
        mov ds,ax

        mov cx,2
        label1:
             mov dl,'a'
             mov ah,2
             int 21h 
             loop label1

        mov ah,4ch
        int 21h
code ends

But my professor is doing something that I find completely ridiculous. He formats the code like below, using unnecessary labels and not using tabs for the labels:

PRINT_HEX PROC 
    H1: MOV BP,SP
        MOV CX,4    
    H2: MOV BX,[BP+2] 
    H3: XOR DL,DL 
        PUSH CX
        MOV CX,4
    H4: SHL BX,1
        RCL DL,1  
    H5: LOOP H4  
        POP CX
    H6: CMP DL,9    
        JA GRAMMA   
        ADD DL,48   
        JMP H7  
    GRAMMA: ADD DL, 55 
    H7: MOV AH,2
        INT 21H
    H8: LOOP H3
    H9: RET
PRINT_HEX ENDP

Of course he obviously knows more than I about assembly but his way seems wrong to me. What is the standard way of formatting code in languages with labels?

Was it helpful?

Solution

I don't think there is a standard style for writting assembly, as there aren't a single standard for writting in C. This said, I find your approach quite reasonable: loops inside the procedure are easily detectable, with the loop counter at the same level of the rest of the loop body, and a blank line to frame visually that code block.

For ancient code written in assembly for 8 bit computers, I've found many times this kind of writting:

code segment
start:  mov ax,data
        mov ds,ax

        mov cx,2
label1: mov dl,'a'
        mov ah,2
        int 21h 
        loop label1

        mov ah,4ch
        int 21h
code ends

I think that the tradition of putting labels starting at the first column goes from the time that assemblers were pretty simple programs that made some assumptions as where labels, instructions and operands should go. My first one, Hisoft DevPac for the ZX Spectrum, assumed that anything written at the first column is a label, and thus, it doesn't need the colon after the label itself. As there are a number of people that learned assembler at that time, they have continued writting it the same way they used to (myself included).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top