Monday, March 14, 2016
8086 Assembly Diamond Print in Console using Loop Explained Code
March 14, 2016
8086 assembly
,
asm
,
condition check
,
diamond print
,
explanation
,
inverted
,
loop
,
made easy
,
triangle print
,
tutorial
Instruction:
It can't handle input of 0 or 1. If you require it then edit and check condition for 0 and 1. If the code seems obscure please debug using 8086 line by line to understand what is changing & how its working.Code:
This file contains 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 |
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment