assembler (10 Green Bottles)
Trevor Simmons submitted:
This assembler code was created for the 8086 processor family, using MASM. This code would be a great way to demonstrate coding in a very low level language. Try to trace how the code runs. Let the comments help.
.model tiny
.code
.startup
; Show the Welcome Message
mov dx, OFFSET welcomemsg ; point to where the message is
mov ah, 9
int 21h ; print the welcome message
mov cx, 2 ; counter = 2
tengreenbottles:
; Print the number 10 onto the screen
mov dl, 31h ; 31h is ASCII for 1
mov ah, 02h
int 21h ; print the number 1 on screen
mov dl, 30h ; 30h is ASCII for 0
mov ah, 02h
int 21h ; print the number 0 on screen
; Show the green bottles on the wall onto screen
mov dx, OFFSET greenbottlesonwall ; point to where the message is
mov ah, 9
int 21h ; print the phase
dec cx ; take one from counter
cmp cx, 0 ; IF counter not equal to 0
jnz tengreenbottles ; loop
mov bl, 39h ; ASCII character 9
bottlesfall:
; Show the what if one falls off onto screen
mov dx, OFFSET andifonefell ; point to where the message is
mov ah, 9
int 21h ; print the phase
; Show the there will be onto screen
mov dx, OFFSET therewillbe ; point to where the message is
mov ah, 9
int 21h ; print the phase
; print the number stored in bl
mov dl, bl ; move what bl stores num of bottles left
mov ah, 02h
int 21h ; print the number on screen
; Show the green bottles on the wall onto screen
mov dx, OFFSET greenbottlesonwall ; point to where the message is
mov ah, 9
int 21h ; print the phase
; go to a new line
mov dl, 13 ; 13h is ASCII for 1
mov ah, 02h
int 21h ; print the number 1 on screen
mov dl, 10 ; 10h is ASCII for 0
mov ah, 02h
int 21h ; print the number 0 on screen
mov cx, 2
nextbottle:
; print the number stored in bl
mov dl, bl ; move what bl stores num of bottles left
mov ah, 02h
int 21h ; print the number on screen
; Show the green bottles on the wall onto screen
mov dx, OFFSET greenbottlesonwall ; point to where the message is
mov ah, 9
int 21h ; print the phase
dec cx
cmp cx, 0
jnz nextbottle
dec bl
cmp bl, 31h
jnz bottlesfall
; Show the what if one falls off onto screen
mov dx, OFFSET andifonefell ; point to where the message is
mov ah, 9
int 21h ; print the phase
; Show the there will be onto screen
mov dx, OFFSET therewillbe ; point to where the message is
mov ah, 9
int 21h ; print the phase
; print the number stored in bl
mov dl, bl ; move what bl stores num of bottles left
mov ah, 02h
int 21h ; print the number on screen
; Show the green bottles on the wall onto screen
mov dx, OFFSET greenbottlesonwall ; point to where the message is
mov ah, 9
int 21h ; print the phase
; go to a new line
mov dl, 13 ; 13h is ASCII for 1
mov ah, 02h
int 21h ; print the number 1 on screen
mov dl, 10 ; 10h is ASCII for 0
mov ah, 02h
int 21h ; print the number 0 on screen
mov cx, 2
lastbottle:
; print the number stored in bl
mov dl, bl ; move what bl stores num of bottles left
mov ah, 02h
int 21h ; print the number on screen
; Show the green bottles on the wall onto screen
mov dx, OFFSET onegreenbottleonthewall ; point to where the message is
mov ah, 9
int 21h ; print the phase
dec cx
cmp cx, 0
jnz lastbottle
; Show the what if one falls off onto screen
mov dx, OFFSET andifonefell ; point to where the message is
mov ah, 9
int 21h ; print the phase
; Show the there will be onto screen
mov dx, OFFSET therewillbe ; point to where the message is
mov ah, 9
int 21h ; print the phase
mov bl, 30h ; ASCII char 0
; print the number stored in bl
mov dl, bl ; move what bl stores num of bottles left
mov ah, 02h
int 21h ; print the number on screen
; Show the green bottles on the wall onto screen
mov dx, OFFSET greenbottlesonwall ; point to where the message is
mov ah, 9
int 21h ; print the phase
; go to a new line
mov dl, 13 ; 13h is ASCII for 1
mov ah, 02h
int 21h ; print the number 1 on screen
mov dl, 10 ; 10h is ASCII for 0
mov ah, 02h
int 21h ; print the number 0 on screen
.exit
.data
welcomemsg DB "Welcome to 10 Green Bottles is Assembler!", 13, 10, 13, 10, '$'
greenbottlesonwall DB " green bottles sitting on the wall!", 13,10, '$'
andifonefell DB "and if one green bottle should accidentally fall", 13, 10, '$'
therewillbe DB "there will be $"
onegreenbottleonthewall DB " green bottle sitting on the wall!", 13,10, '$'
end
------------ OUTPUT -------------
Welcome to 10 Green Bottles is Assembler!
10 green bottles sitting on the wall!
10 green bottles sitting on the wall!
and if one green bottle should accidentally fall
there will be 9 green bottles sitting on the wall!
9 green bottles sitting on the wall!
9 green bottles sitting on the wall!
and if one green bottle should accidentally fall
there will be 8 green bottles sitting on the wall!
8 green bottles sitting on the wall!
8 green bottles sitting on the wall!
and if one green bottle should accidentally fall
there will be 7 green bottles sitting on the wall!
7 green bottles sitting on the wall!
7 green bottles sitting on the wall!
and if one green bottle should accidentally fall
there will be 6 green bottles sitting on the wall!
6 green bottles sitting on the wall!
6 green bottles sitting on the wall!
and if one green bottle should accidentally fall
there will be 5 green bottles sitting on the wall!
5 green bottles sitting on the wall!
5 green bottles sitting on the wall!
and if one green bottle should accidentally fall
there will be 4 green bottles sitting on the wall!
4 green bottles sitting on the wall!
4 green bottles sitting on the wall!
and if one green bottle should accidentally fall
there will be 3 green bottles sitting on the wall!
3 green bottles sitting on the wall!
3 green bottles sitting on the wall!
and if one green bottle should accidentally fall
there will be 2 green bottles sitting on the wall!
2 green bottles sitting on the wall!
2 green bottles sitting on the wall!
and if one green bottle should accidentally fall
there will be 1 green bottles sitting on the wall!
1 green bottle sitting on the wall!
1 green bottle sitting on the wall!
and if one green bottle should accidentally fall
there will be 0 green bottles sitting on the wall!
Comments (0)
You don't have permission to comment on this page.