News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Print an array as a table using Irvine32

Started by adrianvas12, March 01, 2014, 07:17:33 AM

Previous topic - Next topic

adrianvas12

Hello everyone.
I am trying to complete a project where I have to print a WORD array in a table format.
Here is how the array looks like:

NUMS WORD 09EEBh,0B0CFh,061E5h, 089EDh, 0AF17h, 0D8D1h, 06C1Dh, 0594Eh, 0CF55h
WORD 03767h, 063C6h, 0AE84h, 0412Fh, 0B226h, 046C1h, 0879Bh, 076B6h, 093FFh


And here is my code:


TITLE MASM Template (main.asm)


INCLUDE Irvine32.inc
.data
NUMS WORD 09EEBh,0B0CFh,061E5h, 089EDh, 0AF17h, 0D8D1h, 06C1Dh, 0594Eh, 0CF55h
WORD 03767h, 063C6h, 0AE84h, 0412Fh, 0B226h, 046C1h, 0879Bh, 076B6h, 093FFh
sep1 BYTE ", ",0

.code
main PROC
;call Clrscr
mov edi,OFFSET NUMS
mov ecx,LENGTHOF NUMS
mov ebx,TYPE NUMS
mov edx,OFFSET sep1 ;separator character

L1:
mov eax,[edi]
add edi, TYPE NUMS
call WriteHexB
call WriteString
loop L1

call Crlf
exit
main ENDP

END main


I am fairly new to this - every time I run it stops at the first line.
Here is the Run output http://pbrd.co/1hqUFL7

Am I doing something wrong?

Thank you.
Adrian

jj2007

Quote from: adrianvas12 on March 01, 2014, 07:17:33 AM
Am I doing something wrong?

Yes.

First things first: Welcome to the Forum :icon14:

Now to the 'bug': LENGTHOF NUMS refers to all elements labelled NUMS. Now the interesting question is where NUMS ends...

NUMS   WORD 09EEBh,0B0CFh,061E5h, 089EDh, 0AF17h, 0D8D1h, 06C1Dh, 0594Eh, 0CF55h
   WORD 03767h, 063C6h, 0AE84h, 0412Fh, 0B226h, 046C1h, 0879Bh, 076B6h, 093FFh


The blue part is NUMS, the rest is just another data item.

NUMS   WORD 09EEBh,0B0CFh,061E5h, 089EDh, 0AF17h, 0D8D1h, 06C1Dh, 0594Eh, 0CF55h,
   03767h, 063C6h, 0AE84h, 0412Fh, 0B226h, 046C1h, 0879Bh, 076B6h, 093FFh


Now we use the comma as line continuator, and suddenly both lines are NUMS.

adrianvas12

jj2007 - thank you for your reply.
That indeed will solve part of the problem. The next task is to write that array as a table wit 7 rows and 9 columns. Just like I declared it.
How can I do that?


Mark44

Quote from: adrianvas12 on March 01, 2014, 10:48:14 AM
jj2007 - thank you for your reply.
That indeed will solve part of the problem. The next task is to write that array as a table wit 7 rows and 9 columns. Just like I declared it.
How can I do that?
You need two loops - one inside the other. The inner loop processes nine numbers at a time (each number in a particular row). The outer loop processes each of the seven rows.

jj2007

Quote from: Mark44 on March 02, 2014, 02:19:11 PM
You need two loops - one inside the other. The inner loop processes nine numbers at a time (each number in a particular row). The outer loop processes each of the seven rows.

Exactly. Adrian, you can take this snippet as pseudo code:

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  Dim MyR10() As REAL10    ; allocate a REAL10 array
  rows=30                  ; we want 30 rows and 3 columns
  cols=3
  push rows*cols
  xor ecx, ecx             ; start with element zero
  .Repeat
      ; put a random number into Real10 array, between -PI and +PI
      Rand(-3.14159265358979324, PI, MyR10(ecx))
      inc ecx
  .Until ecx>=[esp]        ; loop until ct is bigger than pushed max count
  pop eax                  ; correct the stack
  xor edi, edi             ; start with element zero
  For_ ebx=0 To rows-1
      Print Str$("Row %i", ebx+1)
      For_ ecx=0 To cols-1
            Print Str$("\t%If", MyR10(edi))      ; print three columns
            inc edi
      Next
      Print      ; newline for the next row
  Next
  Inkey "--- hit any key ---"
  Exit
end start

Gunther

Hi adrianvas12,

I think Mark and Jochen didi help to solve your problem. Welcome to the forum.

Gunther
You have to know the facts before you can distort them.

adrianvas12

Thank you for your help !
I was able to print the table somehow - I have another question.

How will I sort this table? I will need to sort each row in ascending sequence.

jj2007


adrianvas12

jj207 - thank you for your suggestion.
I am really new to writing code for this - how will the actual code will look like? Do you have a sample from where I can replicate?

jj2007

Quote from: adrianvas12 on March 04, 2014, 11:24:42 PM
I am really new to writing code for this - how will the actual code will look like? Do you have a sample from where I can replicate?

Sure, here it is. In case you want to understand what it does: google for bubblesort

include \masm32\include\masm32rt.inc

.data
NUMS   WORD 09EEBh,0B0CFh,061E5h, 089EDh, 0AF17h, 0D8D1h, 06C1Dh, 0594Eh, 0CF55h,
   03767h, 063C6h, 0AE84h, 0412Fh, 0B226h, 046C1h, 0879Bh, 076B6h, 093FFh

.code
HexPrint proc arg
  mov eax, arg
  add eax, 4
  print eax, 13, 10
  ret
HexPrint endp

start:
  push lengthof NUMS-1
  mov esi, offset NUMS
  xor ebx, ebx             ; start with element zero
  .Repeat
mov ecx, ebx
.Repeat
mov ax, [esi+2*ebx]
.if ax>[esi+2*ecx]
push word ptr [esi+2*ecx]
mov [esi+2*ecx], ax
pop word ptr [esi+2*ebx]
.endif
inc ecx
.Until ecx>[esp]
      inc ebx
  .Until ebx>=[esp]
  pop eax
  .Repeat
lodsw
invoke HexPrint, hex$(eax)
dec ebx
  .Until Sign?
  inkey "--- OK? ---"
  exit
end start

adrianvas12

Great - I am familiar with sorting algorithms but never coded one in assembly language though. Quick question please: what does the line


.Until Sign?


do? I mean I understand the until but is the Sign?

Thank you.

jj2007

.Until the Sign? flag is set by the preceding dec ebx

adrianvas12


Gunther

You have to know the facts before you can distort them.

adrianvas12

OK - what does it do? And finally please, what about the
inkey "--- OK? ---"? Is that just output? I am using Irvine32.lib and I did not see that command.
Usually we use call WriteString