10 Replies - 2500 Views - Last Post: 02 December 2012 - 11:57 AM

#1 bartekWojcik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-November 12

Pyramid of chars - nasm

Posted 25 November 2012 - 07:38 AM

Hi all,
i have a problem with a program which is doing a pyramid of inputted chars.
for exampled input is 'abc' and output is:
  a
 bbb
ccccc


I'm absolutely new to assembler and everything i have managed to do is:
org 100h
	mov     ah,9
	mov     dx, hello
	int     21h          ; display on screen
	
	mov     ah, 0ah ;takes an char from keybord
	mov     dx, string ; take data
	int     21h
	
	; here should be some loop which i cant figure out
	mov     ah,9
	mov     dx, string+2 ; adress of imputted data
	mov     ax, 4c00h; end of program
	int     21h
	
hello       db "imput an word $"
string      db 20 ;max amount of chars to be takend
            db 0; here we are getting list of chars which were taken
times 22    db "$" ;space for data




I don't know how to make this spaces before chars in these strings. I know only that this pyramid's top line should have n-1 (where n is string length) spaces before an char and every next one is (-1). But i don't know assembler's magic loops and i don't even know how to dismember string to chars here.
of course i was googling for this but even if there were codes that could help me they are for fasm or other ide that i dont understand at all

thanks in advance

This post has been edited by GunnerInc: 25 November 2012 - 10:40 AM
Reason for edit:: Fixed formatting


Is This A Good Question/Topic? 0
  • +

Replies To: Pyramid of chars - nasm

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,276
  • Joined: 28-March 11

Re: Pyramid of chars - nasm

Posted 25 November 2012 - 10:54 AM

Is it a requirement to use DOS?

What code did you find for FASM? A loop in one Assembler, should be the same in another unless they are using high level macros.

Are you learning on your own, or in school? They don't teach you the basics before handing out an assignment?

Here are two tutorials we have on loops, they should help and give you an idea:
http://www.dreaminco...-a-simple-loop/
http://www.dreaminco...516-masm-loops/

I might have code for this somewhere, I will have to look

Look through those tutorials, try to learn from them, write a simple loop that displays 1 character on each line, then try to do what you need to do.
Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,276
  • Joined: 28-March 11

Re: Pyramid of chars - nasm

Posted 25 November 2012 - 11:37 AM

Once you understand loops, you then need to figure out how to draw your pyramid correctly. It would help to draw out a few cases to see what they look like and the position of everything, then you can try to figure out the loop:
abc
  a
 bbb
ccccc

abcd
   a 
  bbb
 ccccc
ddddddd

abcde
    a
   bbb 
  ccccc
 ddddddd
eeeeeeeee

abcdef
     a
    bbb
   ccccc
  ddddddd
 eeeeeeeee
fffffffffff

abcdefg
      a
     bbb
    ccccc
   ddddddd
  eeeeeeeee
 fffffffffff
ggggggggggggg 

Was This Post Helpful? 0
  • +
  • -

#4 bartekWojcik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-November 12

Re: Pyramid of chars - nasm

Posted 25 November 2012 - 12:10 PM

Quote

Is it a requirement to use DOS?

Do u mean interruptions? If yes, then yes :)

Im doing this to school, but we didn't managed to study this 'topic' to the end. And because im involved in others projects in other languages i dont have time to study this one so deeply.

i found these codes at dailyfreecode.com
i.e. http://www.dailyfree...board-1996.aspx

Thanks for links to these tutorials , i will have a look at them as soon as i can.
Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,276
  • Joined: 28-March 11

Re: Pyramid of chars - nasm

Posted 25 November 2012 - 12:18 PM

Read this: http://www.plantatio...oADosIndex.html

Loops are in Chapter 10.6
Was This Post Helpful? 0
  • +
  • -

#6 bartekWojcik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-November 12

Re: Pyramid of chars - nasm

Posted 25 November 2012 - 03:48 PM

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 bartekWojcik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-November 12

Re: Pyramid of chars - nasm

Posted 27 November 2012 - 04:27 PM

This is my actual code but it doesn't work properly.

; program is supposed to write pyramid from string, like this (for abc string)
;  a
; bbb
;cccccc

org 100h
start:
mov ah,9
mov dx, hello
int 21h          ; display on screen

mov ah, 0ah ;takes an char from keybord
mov dx, string ; take data
int 21h

mov bx, dx ; storaging string so he wont be lost after length

push dx ; pushing string on stack
call lenth
pop dx ;lost string but in dx i have it's length

mov ah,9
mov dx, randomsentence ; display a string
int 21h

add dx,48;adding 0 in ascii
mov bx,dx
mov dx,2 ; magic

mov ah,9
mov dx, bx ;string ; adress of imputted data

mov ax, 4c00h; end of program
int 21h

lenth:
pop bx
mov cx,0
dec bx
count:
         inc cx
         inc bx
         cmp byte[ebx],0
         jnz count
dec cx
push bx
ret


hello db "imput an word $"
string db 20 ;max amount of chars to be takend
db 0; here we are getting list of chars which were taken
times 22 db "$" ;space for data
randomsentence db 10,13,10,13, "you wrote $"


Now im just staring at and don't know what to do next. May someone help me write this program properly?
Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,276
  • Joined: 28-March 11

Re: Pyramid of chars - nasm

Posted 28 November 2012 - 07:27 PM

Write the pyramid on paper or an editor with line and column numbers to help you visualize it:
123456789 123456789 123456789 123456789 1234567890
                         a
                        bbb
                       ccccc
                      ddddddd
                     eeeeeeeee
                    fffffffffff
                   ggggggggggggg
                  hhhhhhhhhhhhhhh
                 iiiiiiiiiiiiiiiii
                jjjjjjjjjjjjjjjjjjj
               kkkkkkkkkkkkkkkkkkkkk
              lllllllllllllllllllllll
             mmmmmmmmmmmmmmmmmmmmmmmmm
            nnnnnnnnnnnnnnnnnnnnnnnnnnn
           ooooooooooooooooooooooooooooo
          ppppppppppppppppppppppppppppppp
         qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
        rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
       sssssssssssssssssssssssssssssssssssss
      ttttttttttttttttttttttttttttttttttttttt
     uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
    vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
   wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz


You have a loop to get the length, now try a loop to print out 1 digit without any fomatting. Got that? Next print out 2 digits, then 3, 4, 5 etc... all on separate lines without formatting. Once you have the loop, then you can add inner loops for the formatting.

Writing the code for you does not help you learn.
Was This Post Helpful? 0
  • +
  • -

#9 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,276
  • Joined: 28-March 11

Re: Pyramid of chars - nasm

Posted 29 November 2012 - 09:41 PM

Figure it out?
Was bored waiting for the Nyquil to kick in, so I wrote this to help you. Not the best, and it is not 16bit, but you should get the idea. Not commented, but if needed, I will add comments tomorrow and clean it up when my head is clear.

Windows 32bit NASM, linked with GoLink:
%include "externs.inc"

%define 	STD_INPUT_HANDLE -10
%define		STD_OUTPUT_HANDLE -11
%define		NULL 0

SECTION .data
szPrompt		db	"Ready to make an alphabet pyramid?", 13, 10
				db	"Enter some letters from the alphabet (26 max): ", 0
Prompt_Len		equ	$-szPrompt

szExitPrompt	db	"Press any key to exit", 0
szfmt			db	"%s%s", 0
szCRLF			db	13,10,0

SECTION .bss
buffer			resb 27
pyramid			resb 54
stdin			resd 1
stdout			resd 1
lpNumRead		resd 1

SECTION .text
main:

	push	STD_INPUT_HANDLE
	call	GetStdHandle 
	mov		[stdin], eax
	
	push	STD_OUTPUT_HANDLE
	call	GetStdHandle
	mov		[stdout], eax

.ShowPrompt:
	push	NULL
	push	lpNumRead
	push	Prompt_Len
	push	szPrompt
	push	dword [stdout]
	call	WriteFile
	
	push	NULL
	push	lpNumRead
	push	26
	push	buffer
	push	dword [stdin]
	call	ReadFile
	mov		eax, dword [lpNumRead]
	sub		eax, 2
	test	eax, eax
	jz		.ShowPrompt
	
	mov		byte [buffer + eax], 0			; Repace CRLF with NULL
	mov		dword [lpNumRead], eax
	
	call	ShowPyramid
		
	call	__getch							; To hell with you, it is a Windows app, no cross OS :-p
	ret

ShowPyramid:
%define _CharStartPos [ebp - 4]
%define _CharsToPrint [ebp - 8]
%define _CurLine [ebp - 12]
%define _Pos [ebp - 16]

	push	ebp
	mov		ebp, esp
	sub		esp, 16
	
	mov		eax, dword [lpNumRead]
	mov		dword _CharStartPos, eax

	mov		dword _CurLine, 1
	mov		dword _Pos, 1
	mov		dword _CharsToPrint, 1
	mov		esi, buffer

.NextLine:
	mov		edi, pyramid
	mov		edx, dword _CharStartPos
	dec		edx
	test	edx, edx
	jz		.SpaceDone
	dec		edx

.FillSpaces:
	mov		byte [edi], 32
	inc		edi
	dec		edx
	jns		.FillSpaces
	
.SpaceDone:
	mov		ecx, dword _CharsToPrint		
	mov		al, byte [esi]
	
.FillLine:
	mov		byte [edi], al
	inc		edi
	dec		ecx
	jnz		.FillLine

	push	szCRLF
	push	pyramid
	push	szfmt
	call	printf
	add		esp, 4 * 3
	
	inc		dword _CurLine
	mov		eax, dword _CurLine
	add		eax, dword _Pos
	mov		dword _CharsToPrint, eax
	
	inc		dword _Pos
	dec		dword _CharStartPos
	inc		esi
	mov		eax, dword [lpNumRead]
	cmp		dword _CurLine, eax
	jng		.NextLine
	
	add		esp, 16
	mov		esp, ebp
	pop		ebp
	ret


Attached Image
I am too tired to find the bug that stops printing after 24 lines.

Attached File(s)


Was This Post Helpful? 1
  • +
  • -

#10 bartekWojcik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-November 12

Re: Pyramid of chars - nasm

Posted 02 December 2012 - 09:19 AM

Thanks you very much, thats something i needed.

although i managed to do this (text is in polish, however its quite obvious what it displays):

org 100h

start:
		mov		ah, 9 ; wypisuje tekst aby podac ciag znakow
		mov		dx, podaj
		int		21h

		mov		ah, 0ah ; pobiera ciag znakow
		mov		dx, bufer
		int		21h
		
		xor		bx, bx ; ustawia gdzie jest koniec pobranego ciagu
		mov		bl, [dlugosc]
		mov		byte [tekst+bx+1], '$'
		
		call	wypisz_nl ; nowa linia
		
		mov		dx, pir ; wypisuje tekst piramida
		call	wypisz
		
		call	wypisz_nl
		
		xor		bx, bx ; zeruje bx
		
	piramida:
		cmp		bl, byte [dlugosc] ; jezeli bl == dlugosc ciagu to koniec
		jz		koniec
		
		mov		cl, byte [dlugosc] ; ile razy wypisywac spacje
		sub		cl, bl
		sub		cl, 1
		
		cmp		cl, 0 ; jesli 0 to bez spacji
		jz		bez_spacji
		
	spacje:
		mov		dl, 32 ; wypisywanie spacji
		call 	wypisz_znak
		loop	spacje	
	
	bez_spacji:
		mov		cl, bl ; ile razy wypisywac znak
		add		cl, bl
		add		cl, 1
		
	znaki:
		mov		dl, byte [tekst+bx] ; wypisywanie znaku
		call	wypisz_znak
		loop	znaki
		
		call	wypisz_nl
		
		inc		bx			; dodaje 1 do bx i leci do piramida 
		jmp		piramida
		
koniec:
		mov		dx, czyK ; wypisuje tekst czy koniec
		call	wypisz
		
		call 	wypisz_nl
		
		mov		ah, 0 ; pobiera znak 
		int		16h	
		cmp		al, 35 ; sprawdza czy znak to # jesli nie to leci do start
		jnz		start
		
		mov		ax, 4c00h ; zakonczenie programu
		int		21h
			
wypisz_nl:
	mov		dx, nowalinia
	mov		ah, 9
	int		21h
ret
wypisz:
	mov		ah, 9
	int		21h
ret
wypisz_znak:
	mov		ah, 2
	int		21h
ret

nowalinia	db		13, 10, '$'
bufer		db		20
dlugosc:	db		0
tekst:		times 	22 	db	'$'
podaj		db		"Podaj ciag znakow: $"
pir			db		"Piramida: $"
czyK		db		"Wyjsc z programu? (#)$"


This works properly until 20+ chars are inputted.
Was This Post Helpful? 0
  • +
  • -

#11 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,276
  • Joined: 28-March 11

Re: Pyramid of chars - nasm

Posted 02 December 2012 - 11:57 AM

Ok, I had to translate to English (Sure wish I paid attention when my family spoke Polish when I was growing up)

For future readers of this, this needs to be Assembled as a com file-
makefile:
APP=DosPyramid

Com:
	nasm -f bin -o $(APP).com $(APP).asm


Ran it it DOSBox and figured your problem. I changed one thing to achieve this:

Attached Image

Quote

This works properly until 20+ chars are inputted.


Can you figure out what to change?
Spoiler

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1