Created
March 14, 2016 17:28
-
-
Save quickgrid/abf19435507ef38a5410 to your computer and use it in GitHub Desktop.
Prints diamond on console taking user input. Don't input 0 or 1 otherwise fix code yourself.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;Author:quickgrid | |
;Site: http://quickgrid.blogspot.com | |
;Code: 8086 assembly diamond print dual loop explained code | |
org 100h | |
.stack 100h | |
.data | |
;variable definitions | |
i db ? | |
j db ? | |
k db ? | |
num db '1' | |
counter db 1 | |
input db ? | |
counter2 db ? | |
.code | |
.startup | |
;take input | |
mov ah, 1 | |
int 21h | |
mov j, al | |
;save the input in a variable | |
mov input, al | |
;print new line | |
mov ah, 2 | |
mov dl, 0dh | |
int 21h | |
mov dl, 0ah | |
int 21h | |
;upper triangle loop | |
upper_triangle_loop: | |
;move current outer loop counter to i | |
mov bl, j | |
mov i, bl | |
;skip printing space in the last loop | |
cmp i, '1' | |
je skip_space_loop | |
;loop and print space | |
space_loop: | |
mov dl, ' ' | |
mov ah, 2 | |
int 21h | |
dec i | |
cmp i, '1' | |
jne space_loop | |
skip_space_loop: | |
;reset loop counter to print more numbers | |
mov bl, counter | |
mov k, bl | |
;print output numbers in loop | |
number_print_loop: | |
;check if even or odd | |
mov al, num | |
mov bl, 2 | |
div al | |
;print space if in even positions | |
cmp al, 0 | |
je print_space | |
; print number | |
mov dl, num | |
mov ah, 2 | |
int 21h | |
;if number printing skipped then print space | |
print_space: | |
mov dl, ' ' | |
mov ah, 2 | |
int 21h | |
;exit loop if loop counter is zero | |
dec k | |
cmp k, 0 | |
jg number_print_loop | |
;increment counter to print two more numbers than previous | |
add counter, 1 | |
inc num | |
;print new line | |
mov ah, 2 | |
mov dl, 0dh | |
int 21h | |
mov dl, 0ah | |
int 21h | |
dec j | |
cmp j, '0' | |
jne upper_triangle_loop | |
;reset all previously used variables | |
mov bl, input | |
mov j, bl | |
mov bl, 1 | |
mov counter, bl | |
dec num | |
;loop twice the size of previous value minus one | |
mov bl, input | |
sub bl, 30h | |
mov counter2, bl | |
;print the lower triangle | |
lower_triangle_loop: | |
;replaces i with new counter value | |
mov bl, counter | |
mov i, bl | |
;print spaces | |
space_loop2: | |
mov dl, ' ' | |
mov ah, 2 | |
int 21h | |
dec i | |
cmp i, 0 | |
jne space_loop2 | |
;increment counter to print one more space than previous | |
inc counter | |
mov bl, counter2 | |
mov k, bl | |
dec num | |
;print the numbers | |
number_print_loop2: | |
mov al, num | |
mov bl, 2 | |
div al | |
cmp al, 0 | |
je print_space2 | |
mov ah, 2 | |
mov dl, num | |
int 21h | |
print_space2: | |
mov ah, 2 | |
mov dl, ' ' | |
int 21h | |
dec k | |
cmp k, 1 | |
jne number_print_loop2 | |
dec counter2 | |
;print new line | |
mov ah, 2 | |
mov dl, 0dh | |
int 21h | |
mov dl, 0ah | |
int 21h | |
dec j | |
cmp j, '1' | |
jne lower_triangle_loop | |
;print new line | |
mov ah, 2 | |
mov dl, 0dh | |
int 21h | |
mov dl, 0ah | |
int 21h | |
.exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment