I'm trying to create a bootloader with a christmas tree made of asterisks in assembly, but I can't figure out how to use the same loop to print

Viewed 405

I'm trying to get something like this

    *
   ***
  *****
 *******
*********
   ***
   ***

I had learned how to create a single loop and tried to adapt the same code to use the loop multiple times but failed. Following is the code that I have written so far

[BITS 16] 
[ORG 0x7C00] 
top: 
    ;; Put 0 into ds (data segment) 
    ;; Can't do it directly 
    mov ax,0x0000 
    mov ds,ax 
    ;; si is the location relative to the data segment of the 
    ;; string/char to display 
    mov si, fourSpace
    call writeString
    mov ch, 1 ; mov 1 into the register ch
    call dotsLoop
    mov si, threeSpace
    call writeString
    mov ch, 3 ; mov 3 into the register ch
    call dotsLoop
    mov si, twoSpace
    call writeString
    mov ch, 5 ; mov 5 into the register ch
    call dotsLoop
    mov si, oneSpace
    call writeString
    mov ch, 7 ; mov 7 into the register ch
    call dotsLoop
    mov ch, 9 ; mov 9 into the register ch
    call dotsLoop
    mov si, branchThreeSpace1
    call writeString
    mov ch, 3 ; mov 3 into the register ch
    call dotsLoop
    mov si, branchThreeSpace2
    call writeString
    mov ch, 3 ; mov 3 into the register ch
    call dotsLoop
 
    jmp $ ; Spin 
    
dotsLoop:
    mov si, dot ; print the dot
    call writeString ; See below 
    dec ch ; reduce what is in ch by 1
    cmp ch, 0 ; compare to see if what is store in ch is 0
    jne dotsLoop ; if ch does not contain 0 call dotsLoop again.
    mov si, cr ; print the newline code
    call writeString ; See below 
    ret ; when ch contains 0 return back to main code 
    
    
writeString: 
    mov ah,0x0E ; Display a chacter (as before) 
    mov bh,0x00 
    mov bl,0x07 
nextchar: 
    Lodsb ; Loads [SI] into AL and increases SI by one 
    ;; Effectively "pumps" the string through AL 
    cmp al,0 ; End of the string? 
    jz done 
    int 0x10 ; BIOS interrupt 
    jmp nextchar 
done: 
    ret 
    fourSpace db '    ',0 ; For spacing
    1dot db '*', 13,10,0
    threeSpace db '    ',0 ; For spacing
    3dot db '*',13,10,0
    twoSpace db '    ',0 ; For spacing
    5dot db '*',13,10,0
    oneSpace db '    ',0 ; For spacing
    7dot db '*',13,10,0
    9dot db '*',13,10,0
    branchThreeSpace1 db '    ',0 ; For spacing
    branchdot1 db '*',13,10,0
    branchThreeSpace2 db '    ',0 ; For spacing
    branchdot2 db '*',13,10,0
    times 510-($-$$) db 0
    dw 0xAA55

But, as expected, I am getting the label or instruction expected at start of line error on these lines

1dot db '*', 13,10,0
3dot db '*',13,10,0
etc..

Can anyone guide me on how to use the dotsLoop for more than one instance?

1 Answers
1dot       db '*',13,10,0
3dot       db '*',13,10,0
5dot       db '*',13,10,0
7dot       db '*',13,10,0
9dot       db '*',13,10,0
branchdot1 db '*',13,10,0
branchdot2 db '*',13,10,0

Like Michael Petch wrote in a comment, NASM labels can't start with a digit 0-9.
Next, because all of these lines have the same content, you don't need to repeat yourself 7 times. The CH register will make all the difference that you need.
Additionally, for correct operation in your program, you need to remove that carriage return and linefeed pair from the line.
This is what remains:

dot db '*', 0

fourSpace         db '    ',0 ; For spacing
threeSpace        db '    ',0 ; For spacing
twoSpace          db '    ',0 ; For spacing
oneSpace          db '    ',0 ; For spacing
branchThreeSpace1 db '    ',0 ; For spacing
branchThreeSpace2 db '    ',0 ; For spacing

Here also, you are repeating yourself 6 times! And just as important, you are not outputting the desired number of spaces! Every time you write 4 spaces, thus not producing the christmas tree.

All that you could need is:

fourSpace  db ' '
threeSpace db ' '
twoSpace   db ' '
oneSpace   db ' ', 0

There's really no difference between threeSpace and branchThreeSpace1 or branchThreeSpace2. They should all output precisely 3 space characters.


Can anyone guide me on how to use the dotsLoop for more than one instance?

Apply the above to your code and you'll see that it will work:

...
mov  si, fourSpace
call writeString
mov  ch, 1
call dotsLoop
mov  si, threeSpace
call writeString
mov  ch, 3
call dotsLoop
mov  si, twoSpace
call writeString
mov  ch, 5
call dotsLoop
mov  si, oneSpace
call writeString
mov  ch, 7
call dotsLoop
mov  ch, 9
call dotsLoop
mov  si, threeSpace
call writeString
mov  ch, 3
call dotsLoop
mov  si, threeSpace
call writeString
mov  ch, 3
call dotsLoop
jmp  $ 
dotsLoop:
...
writeString: 
...
dot        db '*', 0
fourSpace  db ' '
threeSpace db ' '
twoSpace   db ' '
oneSpace   db ' ', 0
cr         db 13, 10, 0
...
Related