News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

display format not correct

Started by FlyingDreadnaught, March 02, 2006, 09:00:19 PM

Previous topic - Next topic

FlyingDreadnaught

Hello all, this is my second question here on this helpful forum.  I am taking a class in 80x86 assembly language and this is my first experience with the language.  My previous languages were high level languages so this is all new to me.  Anyway, I'm having a problem that I cannot imagine how to fix and I'm hoping another set of eyes will catch my error.  The program is around 100 lines of code so I don't want to post the whole thing, let me give a summary:

The program finds the first 100 prime numbers using arrays and prints them up in 5 columns.  So, the array and finding the numbers works fine and displays ALMOST correctly.  Here is what should show:

p1 = first prime, p2 = second prime .... p100 = 100th prime

p1                    p2                    p3                     p4                               p5
p6                    p7                    p8                     p9                               p10
.                      .                       .                       .                                  .
.                      .                       .                       .                                  .
p96                 p97                   p98                   p99                             p100


and here is what I'm getting on display:

p1                    p2                    p3                     p4                               p5                             p6
p7                    p8                    p9                     p10                              p11
.                      .                       .                       .                                  .
.                      .                       .                       .                                  .
p97                 p98                   p99                   p100


As I said the numbers are correct, it shows all the primes but the formatting is incorrect.  This is my first experience with arrrays and while I think I'm understanding them, it could be something wrong there.  So, here is the display code:

NOTE you will see some random code commented out with a ;, that's just from me trying different things (and what you see doesn't even begin to show what all I've tried lol)

displayPrimes1:   ;mov   index, 1
      lea   ebx, nbrArray
      mov   eax, [ebx]
      dtoa   outValue, eax
      output   outValue
      lea   ebx, nbrArray
      add   ebx, 4
      mov   eax, [ebx]
      dtoa   outvalue, eax
      output   outValue
      mov   index, 2
      

displayPrimes2:   
      cmp   index, 100      ; for index := 1 to 100
      je   ExitProcess         
      
nextAddress:   mov   eax, index
      imul   eax, 4
      mov   address, eax
      lea   ebx, nbrArray
      add   ebx, address
      mov   eax, [ebx]
      ;jmp   display


display:   ;inc   index
      dtoa   outValue, eax
      output   outValue
      mov   ebx, index
      mov   edx, 0
      ;mov   ebx, index
      ;inc   index
      mov   eax, index
      mov   ecx, 5
      idiv   ecx
      cmp   edx, 0
      jz   newLine
      inc   index
      jmp   displayPrimes2

newLine:   output   lineBreak
      inc   index
      jmp   displayPrimes2



So, please, someone take a look at this and tell me what I'm missing because I've tried everything that I can think of.

Mark Jones


displayPrimes1:   ;mov   index, 1
      lea   ebx, nbrArray
      mov   eax, [ebx]
      dtoa   outValue, eax
      output   outValue
      lea   ebx, nbrArray
      add   ebx, 4
      mov   eax, [ebx]
      dtoa   outvalue, eax
      output   outValue
      mov   index, 2

0,1,2...  ?
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

FlyingDreadnaught

I'm not sure if you are asking why start with 2 or what, but the index starts at 2 because the first 2 primes are given in the problem and I was supposed to find the remaining 98.  I figured maybe it would work if I change the start value of index to 3, and it alsmost does.  It displays properly except it skips prime[3] which is 5, so it displays:

2           3               7                 11           13


and the bottom row is still missing last number.

dsouza123

reordered a little, with maximum column logic

    mov col, 0
    mov index, 0
    lea ebx, nbrArray
displayPrimes:
    mov eax, [ebx]
    dtoa outvalue, eax
    output outValue
    inc col
    cmp col, 5
    jne sameLine
    output lineBreak
    mov col, 0
sameLine:
    add ebx, 4
    inc index
    cmp index, 100
    jne displayPrimes


mostly like the following pascal code,
also the array starts at item 0 just as in assembly

col   := 0;
index := 0; 
    b := 0;
repeat
  write(nbrArray[b]);
  inc(col);
  if col = 5 then begin
    writeln;
    col := 0;
  end;
  b := b + 1;
  inc(index);
until index = 100;

dsouza123

#4
As close as possible to the original code


    mov index, 0
    lea ebx, nbrArray
displayPrimes:
    mov eax, [ebx]
    dtoa outvalue, eax
    output outValue
    inc index
    mov edx, 0        ; added for correct calculations
    mov eax, index
    mov ecx, 5
    div ecx
    cmp edx, 0
    jne sameLine
    output lineBreak
sameLine:
    add ebx, 4
    cmp index, 100
    jne displayPrimes

asmfan

ok, guys... the first thing that hurts my eyes is that nobody prepares the register EDX before division!
div ecx    ;divides edx : eax by ecx
i used to fail on this for my assembly youth:)
Russia is a weird place

FlyingDreadnaught

Well thanks for all of the input guys, it got me thinking about approaching the problem in a different manner and after about 30 minutes of fiddling around with it, I had a working solution that was much simpler and cleaner.  Here is the code I used:

loadArray:
      lea   ebx, nbrArray
      mov   index, 1
      mov   nbrDisplays, 1
      
displayPrimes:
      
      cmp   nbrDisplays, 100
      jg   ExitProcess
      mov   eax, [ebx]
      dtoa   outValue, eax
      output   outValue
      inc   index
      inc   nbrDisplays
      cmp   index, 5
      jg   newLine
      add   ebx, 4
      jmp   displayPrimes
      


newLine:   output   lineBreak
      add         ebx, 4
      mov          index, 1
      jmp        displayPrimes



P.S. to asmfan:

If you noticed in my original code, I DID clear the edx before division heh  :dance: