Author Topic: Print an array as a table using Irvine32  (Read 8422 times)

adrianvas12

  • Guest
Print an array as a table using Irvine32
« on: March 01, 2014, 07:17:33 AM »
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:
Code: [Select]
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:

Code: [Select]
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

  • Member
  • *****
  • Posts: 7728
  • Assembler is fun ;-)
    • MasmBasic
Re: Print an array as a table using Irvine32
« Reply #1 on: March 01, 2014, 09:18:05 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

  • Guest
Re: Print an array as a table using Irvine32
« Reply #2 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?


Mark44

  • Regular Member
  • *
  • Posts: 40
Re: Print an array as a table using Irvine32
« Reply #3 on: March 02, 2014, 02:19:11 PM »
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

  • Member
  • *****
  • Posts: 7728
  • Assembler is fun ;-)
    • MasmBasic
Re: Print an array as a table using Irvine32
« Reply #4 on: March 02, 2014, 07:19:14 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

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: Print an array as a table using Irvine32
« Reply #5 on: March 02, 2014, 11:15:34 PM »
Hi adrianvas12,

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

Gunther
Get your facts first, and then you can distort them.

adrianvas12

  • Guest
Re: Print an array as a table using Irvine32
« Reply #6 on: March 03, 2014, 02:12:06 PM »
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

  • Member
  • *****
  • Posts: 7728
  • Assembler is fun ;-)
    • MasmBasic
Re: Print an array as a table using Irvine32
« Reply #7 on: March 03, 2014, 05:16:24 PM »
Bubblesort would be easy to do.

adrianvas12

  • Guest
Re: Print an array as a table using Irvine32
« Reply #8 on: March 04, 2014, 11:24:42 PM »
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

  • Member
  • *****
  • Posts: 7728
  • Assembler is fun ;-)
    • MasmBasic
Re: Print an array as a table using Irvine32
« Reply #9 on: March 05, 2014, 12:18:19 AM »
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

Code: [Select]
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

  • Guest
Re: Print an array as a table using Irvine32
« Reply #10 on: March 05, 2014, 12:33:45 AM »
Great - I am familiar with sorting algorithms but never coded one in assembly language though. Quick question please: what does the line

Code: [Select]
.Until Sign?

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

Thank you.

jj2007

  • Member
  • *****
  • Posts: 7728
  • Assembler is fun ;-)
    • MasmBasic
Re: Print an array as a table using Irvine32
« Reply #11 on: March 05, 2014, 12:40:48 AM »
.Until the Sign? flag is set by the preceding dec ebx

adrianvas12

  • Guest
Re: Print an array as a table using Irvine32
« Reply #12 on: March 05, 2014, 12:46:30 AM »
Is Sign a variable?

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: Print an array as a table using Irvine32
« Reply #13 on: March 05, 2014, 01:01:13 AM »
Is Sign a variable?

It's a flag.

Gunther
Get your facts first, and then you can distort them.

adrianvas12

  • Guest
Re: Print an array as a table using Irvine32
« Reply #14 on: March 05, 2014, 01:07:47 AM »
OK - what does it do? And finally please, what about the
Code: [Select]
inkey "--- OK? ---"? Is that just output? I am using Irvine32.lib and I did not see that command.
Usually we use
Code: [Select]
call WriteString