Below is the code for Drawing a triangle in assembly language by taking the number of rows as input from the User.
Example :: When the program starts it will prompt for the number of rows for the triangle you want to display. After you enter the number of rows The Triangle of *'s is displayed on the screen.
Note :: This is assembly language code for Computer x86 Architecture not for MIPS. This code is tested on DOSBOX . You can also run it using NASM as well. The complete code for printing the triangle is given below .
Just copy the code and compile it and You are done. Thanks
-------------------------------- Code Below --------------------------------------
; Assembly language program to display a triangle
[org 0x0100]
jmp start
message: db '*' ; string to be printed
length: dw 5 ; length of the string
line_width: dw 160
stars_perline: dw 1
num_of_lines: dw 3
reduce_space: dw 2
; subroutine to clear the screen
clrscr: push es
push ax
push di
; Copyright :: Farhan Maqsood
; Phone (+923084245492)
; Email farhangdon009@gmail.com
; This is for learning Purpose
; All code given is tested on DOSBOX
mov ah,0
int 0x16
cmp al,0x31
je update1
cmp al,0x32
je update2
cmp al,0x33
je update3
cmp al,0x34
je update4
cmp al,0x35
je update5
cmp al,0x36
je update6
cmp al,0x37
je update7
cmp al,0x38
je update8
cmp al,0x39
je update9
jmp back_to_work
update1:
mov bx,1
mov [num_of_lines],bx
jmp back_to_work
update2:
mov bx,2
mov [num_of_lines],bx
jmp back_to_work
update3:
mov bx,3
mov [num_of_lines],bx
jmp back_to_work
update4:
mov bx,4
mov [num_of_lines],bx
jmp back_to_work
update5:
mov bx,5
mov [num_of_lines],bx
jmp back_to_work
update6:
mov bx,6
mov [num_of_lines],bx
jmp back_to_work
update7:
mov bx,7
mov [num_of_lines],bx
jmp back_to_work
update8:
mov bx,8
mov [num_of_lines],bx
jmp back_to_work
update9:
mov bx,9
mov [num_of_lines],bx
back_to_work :
mov ax, 0xb800
mov es, ax ; point es to video base
mov di, 0 ; point di to top left column
nextloc: mov word [es:di], 0x0720 ; clear next char on screen
add di, 2 ; move to next screen location
cmp di, 4000 ; has the whole screen cleared
jne nextloc ; if no clear next position
pop di
pop ax
pop es
ret
; subroutine to print a string at top left of screen
; takes address of string and its length as parameters
printstr: push bp
mov bp, sp
push es
push ax
push cx
push si
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov di, 50 ; point di to top left column
mov si, [bp+6] ; point si to string
mov cx, [bp+4] ; load length of string in cx
mov ah, 0x07 ; normal attribute fixed in al
mov cx,30;
;---------------------------- working here ------------------------------;
;nextchar: mov al, [si] ; load next char of string
;mov [es:di], ax ; show this char on screen
;add di, 2 ; move to next screen location
;add si, 1 ; move to next char in string
;loop nextchar ; repeat the operation cx times
mov bx,[num_of_lines]
mov ax,160
again_please :
cmp bx,0
je this_is_exit
sub bx,1
mov cx,[stars_perline]
sub ax,di
add di,ax
add di,50
mov dx,di
sub dx,[reduce_space]
mov di,dx
mov ah, 0x07
nextchar2: mov al, [si] ; load next char of string
mov [es:di], ax ; show this char on screen
add di, 2 ; move to next screen location
;add si, 1 ; move to next char in string
loop nextchar2 ; repeat the operation cx times
mov ax,[line_width]
add ax,160
mov [line_width],ax
mov dx,[reduce_space]
add dx,2
mov [reduce_space],dx
mov dx,[stars_perline]
add dx,2
mov [stars_perline],dx
jmp again_please
this_is_exit:
pop di
pop si
pop cx
pop ax
pop es
pop bp
ret 4
start: call clrscr ; call the clrscr subroutine
mov ax, message
push ax ; push address of message
push word [length] ; push message length
call printstr ; call the printstr subroutine
mov ax, 0x4c00 ; terminate program
int 0x21
--------------------------------- Code Ends Here ------------------------------------------
Let me know if You have any issues. Thanks
Example :: When the program starts it will prompt for the number of rows for the triangle you want to display. After you enter the number of rows The Triangle of *'s is displayed on the screen.
Note :: This is assembly language code for Computer x86 Architecture not for MIPS. This code is tested on DOSBOX . You can also run it using NASM as well. The complete code for printing the triangle is given below .
Just copy the code and compile it and You are done. Thanks
-------------------------------- Code Below --------------------------------------
; Assembly language program to display a triangle
[org 0x0100]
jmp start
message: db '*' ; string to be printed
length: dw 5 ; length of the string
line_width: dw 160
stars_perline: dw 1
num_of_lines: dw 3
reduce_space: dw 2
; subroutine to clear the screen
clrscr: push es
push ax
push di
; Copyright :: Farhan Maqsood
; Phone (+923084245492)
; Email farhangdon009@gmail.com
; This is for learning Purpose
; All code given is tested on DOSBOX
mov ah,0
int 0x16
cmp al,0x31
je update1
cmp al,0x32
je update2
cmp al,0x33
je update3
cmp al,0x34
je update4
cmp al,0x35
je update5
cmp al,0x36
je update6
cmp al,0x37
je update7
cmp al,0x38
je update8
cmp al,0x39
je update9
jmp back_to_work
update1:
mov bx,1
mov [num_of_lines],bx
jmp back_to_work
update2:
mov bx,2
mov [num_of_lines],bx
jmp back_to_work
update3:
mov bx,3
mov [num_of_lines],bx
jmp back_to_work
update4:
mov bx,4
mov [num_of_lines],bx
jmp back_to_work
update5:
mov bx,5
mov [num_of_lines],bx
jmp back_to_work
update6:
mov bx,6
mov [num_of_lines],bx
jmp back_to_work
update7:
mov bx,7
mov [num_of_lines],bx
jmp back_to_work
update8:
mov bx,8
mov [num_of_lines],bx
jmp back_to_work
update9:
mov bx,9
mov [num_of_lines],bx
back_to_work :
mov ax, 0xb800
mov es, ax ; point es to video base
mov di, 0 ; point di to top left column
nextloc: mov word [es:di], 0x0720 ; clear next char on screen
add di, 2 ; move to next screen location
cmp di, 4000 ; has the whole screen cleared
jne nextloc ; if no clear next position
pop di
pop ax
pop es
ret
; subroutine to print a string at top left of screen
; takes address of string and its length as parameters
printstr: push bp
mov bp, sp
push es
push ax
push cx
push si
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov di, 50 ; point di to top left column
mov si, [bp+6] ; point si to string
mov cx, [bp+4] ; load length of string in cx
mov ah, 0x07 ; normal attribute fixed in al
mov cx,30;
;---------------------------- working here ------------------------------;
;nextchar: mov al, [si] ; load next char of string
;mov [es:di], ax ; show this char on screen
;add di, 2 ; move to next screen location
;add si, 1 ; move to next char in string
;loop nextchar ; repeat the operation cx times
mov bx,[num_of_lines]
mov ax,160
again_please :
cmp bx,0
je this_is_exit
sub bx,1
mov cx,[stars_perline]
sub ax,di
add di,ax
add di,50
mov dx,di
sub dx,[reduce_space]
mov di,dx
mov ah, 0x07
nextchar2: mov al, [si] ; load next char of string
mov [es:di], ax ; show this char on screen
add di, 2 ; move to next screen location
;add si, 1 ; move to next char in string
loop nextchar2 ; repeat the operation cx times
mov ax,[line_width]
add ax,160
mov [line_width],ax
mov dx,[reduce_space]
add dx,2
mov [reduce_space],dx
mov dx,[stars_perline]
add dx,2
mov [stars_perline],dx
jmp again_please
this_is_exit:
pop di
pop si
pop cx
pop ax
pop es
pop bp
ret 4
start: call clrscr ; call the clrscr subroutine
mov ax, message
push ax ; push address of message
push word [length] ; push message length
call printstr ; call the printstr subroutine
mov ax, 0x4c00 ; terminate program
int 0x21
--------------------------------- Code Ends Here ------------------------------------------
Let me know if You have any issues. Thanks
Hi, i took errors in this lines:
ReplyDelete[org 0x0100] ; i make it org 100h it works
mov [es:di], ax ; show this char on screen
push word [length] ; push message length
how can i fix these and i'm using emu8086.Is it make difference?
same error yar ,,,,
Deleteif u know the solution can u kindly send to me in mail...
sabbisetti.gowthamkumar@gmail.com
thank you
This comment has been removed by the author.
Delete