Numbering lines in assembly

68 Views Asked by At

my problem is that I need to number lines according to the following assignment among some other tasks. I managed to do all the others, but I don't know how to do this one. Number the non-empty lines passed to the output with sequential numbers in the decimal system starting with the number 1. There will never be more than 99 lines to number. You always place two digits from the beginning of the line followed by a period and a space. If the line number is in the range 1-9, you place a space before the number. example: _1._text 10._text

this is the part where I tried to do it, but without success. I thought about using stack, but I wanted to ask for advice first.


...

nums    inc cx
;   mov bx, tens
;   cmp bx, 50
;   ja lower
;both   mov dx, ones
;   mov ah, 9
;   int 21h
;   inc dx
    mov dx, bfrtxt
    mov ah, 9
    int 21h
;   mov dx, ones
;   cmp dx, 59
;   je reset
fork    test cx,1
    jz oddw1
    jnz evenw1
    
;lower  mov dx, ' '
;   mov ah, 9
;   int 21h
;   jmp both
;   
;higher mov dx, tens
;   mov ah, 9
;   int 21h
;   cmp dx, 9
;   jmp both
    
;reset  mov dx, 49
;   jmp fork

...

segment data
    resb 1024
ones    db 50
tens    db 49
line    db 0
bfrtxt  db '. ','$'
1

There are 1 best solutions below

0
Nassau On

This code only generates line numbers from 1 to 23 just to fit the screen. Change max_lines to 99.

Line number is stored in line_num. Code modifies only bytes at offset line_num and line_num + 1.

How to combine text with line number is up to You. ;)

.model small
.stack 0ffh
.data
    line_num db ' ','1','.',' ',13,10,'$'
    max_lines db 23

.code
main proc
    mov ax,@data
    mov ds,ax

    xor cx,cx
    mov cl,1                            ; line counter  
    mov bx,offset line_num              ; stores line number
    
    show_num:
        mov ah,09h                      ; print line number
        mov dx,bx
        int 21h
        
        inc cl                          ; next line number
        
        cmp cl,9                        ; less or equal 9
        jbe le_9                        
        
        gt_9:
            mov ax,cx                   ; greater than 9    
            mov dl,10
            div dl                      ; al = quotient, ah = remainder
        
            add al,30h                  ; add 30h to al and ah 
            mov [bx],al                 ; because we want ascii '0'-'9'
            inc bx
            add ah,30h
            mov [bx],ah 
            dec bx
        
            jmp next_line
        
        le_9:
            add cl,30h                  ; ascii '1' - '9'
            inc bx                      ; line_num + 1                      
            mov [bx],cl
            dec bx          
            sub cl,30h          
            
            next_line:
                cmp cl, [max_lines]     
                jbe show_num
        
    exit:
        mov ax,4c00h
        int 21h

main endp
end main