Subscribe to Bodom's Universe        RSS Feed
-----

Past, Present, and Future (and.... a christmas tree in NASM)

Icon Leave Comment
So, a lot of bloggers are probably posting something tonight about Christmas, and giving you code to print off a Christmas tree, or whatever else. You know, things that have been done a million times before. Well, before I do that, I'm going to talk a bit about the past, present, and future of this blog. If you have no interest in this, just skip down till you start seeing code snippets. I've got no itinerary on this blog post. Moreover, I'm bored and it's been a while.

If you go back and look through what I've blogged about in the past, you'll see a variety of topics, such as computer administration in both windows and Linux, C++, Java, Bada, and a few other things. I like to change it up a bit. When I started this blog, it was more about the fact that a lot of other people were doing it and I wanted to be able to add something, to get my own 2 cents in.

Presently, coding wise, I have taken a larger interest in MATLAB and in embedded computing, though with work bogging me down with enough coding, I never really have time to do anything on my own. Thus, it's been a while since I have made a single post.

In the future, I will be taking several courses involved with embedded and network computing, so there will be blogs situated in this light. I do think that this is the direction that I would like to take with this blog. I am a Computer Engineer by major, so it makes sense that I'd be a bit more technical in the electronics sense then a lot of other bloggers here, and pay more attention to hardware design then they typically would. I do realize that dream.in.code is a programming website, so the vast majority of content in these entries will be code and the explanation of the syntax and implementation, but hey, I like talking people through building a multiplexer and hooking it up to a microcontroller, and then walking you through the code to getting the thing running.

However, I also realize that not everyone can go out and buy a microcontroller and programmer, so I'll be doing systems programming stuff as well, which is an interest to me much greater than that of application programming.

One of the great systems programming languages is assembly. Personally, I use NASM on Windows 7 x86. The following should compile on any windows machine with NASM, and should only need slight modification to get running on say, a linux system.

Starting our program at 0x100 is basically a requirement. Work with me here.
org 0x100 ; Good place to start on a windows system



Now, we're going to need a method that will print a character.
The code is commented and should be easy enough:
printchar:
    mov ah,0x0E    ; We're writing a character to the console
    mov bx,0x0007  ; Page 0x00 (Dont change), normal color
    int 0x10       ; BIOS Video Interrupt
    retn           ; Return from printchar



Now, let's keep in mind that the code above assumes the character we want to print is in al.

So we really have 2 options. We can make separate strings and print them off one at a time, or we can print a set amount of spaces each time, and then a certian number of #'s which I'll use to represent the tree.

Basically we want:
     #
    ###
   #####
     #



so, 5 spaces, 1 #; 4 spaces, 3#; 3 spaces 5#; 5 spaces, 1#.

We may as well use strings to be honest. It gives you another useful function. I call it 'printstring'
So, we're going to create strings and use the delimiter 0.
Something like:
str1 db '     #',0
str2 db '    ###',0
str3 db '   #####',0



Notice, the first and last are the same. Notice also, I left out a character, the '\n'!
this is no problem, we'll print that off automatically after each string in our function (You can take out the printchar call doing this and the line setting al to '\n' to get rid of this new line). Note also that this function also assumes that your string is stored in si.

So, here's printstring:
printstring:
    mov byte al,[si]     ; Move the byte [si] to al
    or al,al             ; See if we have a zero
    jz .end              ; End if we do
    call printchar       ; Print it
    inc si               ; increase our index
    jmp printstring      ; Go to start of function
.end mov al,0x0A         ; Load up the newline character
    call printchar       ; Print it
    retn                 ; Return to calling function



So now, all we do is call the function 4 times, and exit properly.

So, stick this into the file, right under the [org... line
mov si,str1       ; Load a string
call printstring  ; print it
mov si,str2       ; etc...
call printstring
mov si,str3
call printstring
mov si,str1
call printstring
mov ax,0x4C00    ; ah -> exit function, al, exit code
int 0x21         ; Windows Kernel interrupt



Now you should have a full program that looks something like this:
org 0x100

	; 5 spaces, 1 #
	mov si,str1
	call printstring
	; 4 spaces, 3 #
	mov si,str2
	call printstring
	; 3 spaces, 5 #
	mov si,str3
	call printstring
	; 5 spaces, 1 $
	mov si,str1
	call printstring
	mov ax,0x4C00
	int 0x21

printchar:
	mov ah,0x0E
	mov bx,0x0007
	int 0x10
	retn

printstring:
	mov al,[si]
	or al,al
	jz .end
	inc si
	call printchar
	jmp printstring
.end	mov al,0x0A
	call printchar
	retn

str1 db '     #',0
str2 db '    ###',0
str3 db '   #####',0



(For comments, read above, haha!)

And if you compile and run it, you should see the tree as printed above. To compile use:
nasm -f bin -o tree.exe tree.asm



And then run it in a command shell:
.\tree



Happy Holidays!
~Bodom

0 Comments On This Entry

 

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Recent Comments

Search My Blog

21 user(s) viewing

21 Guests
0 member(s)
0 anonymous member(s)