Author Topic: array  (Read 36563 times)

Offline dstudentx

  • Jr. Member
  • *
  • Posts: 18
array
« on: February 21, 2010, 06:13:25 AM »
How the heck to you implement an array! I've been lost for hours
any help would be greatly appreciated.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: array
« Reply #1 on: February 21, 2010, 08:47:41 AM »
Code: [Select]
msg db "Hello, world!", 0

:)

Well, it's an array. Of bytes. What did you want an array of?

Code: [Select]
array dd 1, 2, 3, 4
array2 dq 1.0, 2.0, 3.14, 42.0

A multidimensional array? No such thing - lay it out "flat" and address it as "row * row_length + column * item_size".

What are you trying to do? (and what happens?)

Best,
Frank


Offline Flybro

  • Jr. Member
  • *
  • Posts: 6
  • Country: au
Re: array
« Reply #2 on: February 21, 2010, 09:34:06 AM »
Take a look at http://www.drpaulcarter.com/pcasm/
There is long chapter in the manual about arrays.

Cheers,
F.

Offline dstudentx

  • Jr. Member
  • *
  • Posts: 18
Re: array
« Reply #3 on: February 21, 2010, 06:14:27 PM »
sorry.

I simple array of integers.
the user inputs say 

enter a number: 3
enter another number? Y
enter a number: 4
enter another number? Y
enter a number: 5
enter another number? N
you entered 3 4 5

I don't know how to store the user inputs in an array

Offline dstudentx

  • Jr. Member
  • *
  • Posts: 18
Re: array
« Reply #4 on: February 21, 2010, 07:12:22 PM »
please help im dying here.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: array
« Reply #5 on: February 21, 2010, 10:53:59 PM »
Well, first reserve some memory to store it in. Since one problem you'll want to avoid is overrunning the buffer, best to "%define" a size we can compare to.

Code: [Select]
%define ARRAY_SIZE 3

section .bss
my_array resd ARRAY_SIZE

Note that this is in dwords, not bytes. You only show 3, Dr. Carter uses 100 - somewhere in between is probably what you want.

Now you need a means of obtaining some numbers to populate it with. You got that? Curiously, Dr. Carter's example calls scanf directly rather than use his "read_int". Dunno why. In any case, s'pose you've got the number in eax. You'll want to know which array element it's going in - s'pose in ecx. Keep in mind that the numbering starts at zero! If, for the benefit of zero-impaired "users", you want to let them call the zeroth element "1", you can make the adjustment, but the "real" numbering starts at zero. This means that the "last" element is "ARRAY_SIZE - 1"!

Code: [Select]
cmp ecx, ARRAY_SIZE - 1
ja no_good_index
mov [my_array + ecx * 4], eax

What you want to do at "no_good_index:" depends... If you're just filling the array sequentially, it means you're done. If you're asking the user for an element number, ask again.

To fetch 'em back, of course...

Code: [Select]
cmp ecx, ARRAY_SIZE -1
ja no_good_index_2
mov eax, [my_array + ecx * 4]

If you're calling C, keep in mind that C can trash ecx "behind your back" (if you've got your back turned). You might want to use another register if that's a problem - or push ecx before the C call and pop it after... as Dr. Carter does in his example.

Does that help? If not, which parts are giving you trouble?

Best,
Frank


Offline travman89

  • Jr. Member
  • *
  • Posts: 4
Re: array
« Reply #6 on: February 22, 2010, 02:37:06 AM »
I was wondering if you could post a very basic walkthrough of arrays. I have this same type of problem for my assignment, and I have tried to follow dr carter's example but I do not know how to use the push and pop stack commands. Is there any way you can show the entire input and output commands. I have a grasp on loops, but the arrays are just stumping me.

Offline dstudentx

  • Jr. Member
  • *
  • Posts: 18
Re: array
« Reply #7 on: February 22, 2010, 03:32:23 AM »
I really appreciate the help but this is essentially my first program and I'm have problems everywhere.  I sample posting of code for and input of numbers would really help. 
I'm just really lost the Nasm.

Offline dstudentx

  • Jr. Member
  • *
  • Posts: 18
Re: array
« Reply #8 on: February 22, 2010, 03:41:35 AM »
This is how lost I am

Code: [Select]

;
; file: first.asm


%include "asm_io.inc"
;
; initialized data is put in the .data segment
;
segment .data
;
; These labels refer to strings used for output
;
%define ARAY_SIZE 20
prompt1 db    "Please enter a number ", 0
outmsg1 db    "You entered ", 0



;
; uninitialized data is put in the .bss segment
;
segment .bss
;
; These labels refer to double words used to store the inputs
;
input1  resd ARRAY_SIZE
input2  resd 1


;
; code is put in the .text segment
;
segment .text
        global  asm_main
asm_main:
        enter   0,0               ; setup routine
        pusha


        mov     eax, prompt1      ; print out prompt
        call    print_string

        call    read_int          ; read integer
        mov     [input2], eax     ; store into input1

cmp ecx, ARRAY_SIZE -1
        mov [input1 + ecx * 4], eax

cmp ecx, ARRAY_SIZE -1
mov [input1 + ecx * 4], eax

       
        dump_regs 1               ; dump out register values
        dump_mem 2, outmsg1, 1    ; dump out memory
endwhile:
;
; next print out result message as series of steps
;
        mov     eax, outmsg1
        call    print_string      ; print out first message
        mov     eax, [input1]     
        call    print_int         ; print out input1
           
       
       
        call    print_nl          ; print new-line

        popa
        mov     eax, 0            ; return back to C
        leave                     
        ret

Offline travman89

  • Jr. Member
  • *
  • Posts: 4
Re: array
« Reply #9 on: February 22, 2010, 03:56:07 AM »
Does the program execute?  If it does, does the loop work?  What is the output?

Offline dstudentx

  • Jr. Member
  • *
  • Posts: 18
Re: array
« Reply #10 on: February 22, 2010, 04:00:26 AM »
are you kidding me.  it doesn't work at all.   I think in paul carters code for prime.asm we can modify it to work.  prime.asm has a while loop.  I think if we can get array input in the while loop where pretty much done.

travman89 do you go to CSUF?

Offline lukus001

  • Jr. Member
  • *
  • Posts: 16
Re: array
« Reply #11 on: February 22, 2010, 05:52:56 AM »
dstudentx,

Ive not used %define or the like, as I myself am pretty new so I can't vouch for accuracy, but hopefully the following will be of help to you?

You've defined 'Array_size' as 20, and used that to reserve 20 dwords of space in your .bss section 'input1'.  After you got your input you store the value from eax into 'input2' which is only a single dword of allocated space (that seems backwards to me and your comment says input1, not input2).

You then compare ecx (not sure what is in ecx) to 'array_size -1' which would be 19 by all accounts?  After the comparison, there is also no conditional jumps so it would do your 'mov' instruction regardless of your cmp instruction.  You then have the same code (cmp followed by a mov) which is unnecessary since nothing has changed between the two.   Not sureon the purpose of these mov's either.

You then print your outmessage, followed by printing 'input1' which I suspect is correct, if the above was corrected from input2 to input1.

You probabily want something more along these lines :
Code: [Select]
;you will need to add to .data:
prompt2 db 'would you like to add another number? Please press y or n',0


asm_main:
        enter   0,0               ; setup routine
        pusha

mov edx,0 ;reset our counter

        mov     eax, prompt1      ; print out prompt
        call    print_string

        call    read_int          ; read integer

mov  edi,input1 ;pointer to input

ask_number:
inc dword edx ;start the counter
cmp edx,21 ;does edx=21?
je end ;reached maximum size od storage space (array_size 20;))
stosb ;store value (eax only) into adress space edi(input1) by a single byte (change to stosd for dword) this instruction also increases edi to next address space automatically


mov eax, prompt2 ;add another?
call print_string

add_another:
call read_int ;read a 'y' or 'n' character, not really an int though, subsitute where necessary, possibly read_char?.

cmp eax,'y' ;did they say yes?
je ask_number ;if so jump to ask number

cmp eax,'n' ;otherwise did they say no?
jne add_another ;user did not select yes or no, repeat this again.
jmp end ; user selected no, end program.

end:
;print array as one block of numbers, by pointing to input1 but you should try to space them out as single numbers i.e. 1, 2, 3, 4, 5 rather than 12345
;if none of the calling references affect edx, then edx has the number of values stored in the simple array. inlinux you need to specify how long the message to be printed is via std-out

Hope this helps.   Looks like you are coding for windows and im on linux so not sure what your fucntions do exactly








« Last Edit: February 22, 2010, 05:59:11 AM by lukus001 »

Offline dstudentx

  • Jr. Member
  • *
  • Posts: 18
Re: array
« Reply #12 on: February 22, 2010, 05:58:36 AM »
its coding using Dr Paul Carters guidelines.
thank  you very much for the help.
I'll try to figure it out and use it

hey travman89 did you get me message?  I think I'm having problem replying.  if so send me your email.

Offline dstudentx

  • Jr. Member
  • *
  • Posts: 18
Re: array
« Reply #13 on: February 22, 2010, 06:04:39 AM »
I think I got the input of the array but now how do I output the results?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: array
« Reply #14 on: February 22, 2010, 06:58:18 AM »
Well, how does it "not work at all"? Looks to me like ARAY_SIZE vs ARRAY_SIZE might be a problem. If you fix that, it'll at least assemble. Then it promptly segfaults. What could be the problem?

cmp ecx, ARRAY_SIZE -1

The "cmp" doesn't do anything without a "jcc" after it!

mov [input1 + ecx * 4], eax

What's in ecx? Random garbage! For your first, or zeroth, element, you want to start with ecx = 0, so "xor ecx, ecx" first. You'll be wanting to repeat the following code, so put a label after it.

    xor ecx, ecx
.top:

Then you go on to...

        mov     eax, prompt1      ; print out prompt
        call    print_string

        call    read_int          ; read integer
        mov     [input2], eax     ; store into input1

Well, you don't want to store it in the same place every time. Replace that with:
   
        mov [input1 + ecx * 4], eax

I guess you're done with that element. Go on to the next one.

        inc ecx

Make sure we're not done...
   
   cmp ecx, ARRAY_SIZE -1
        jna .top

Do pretty much the same thing to your output routine, and you've got a "working" program. It doesn't match the specification yet - you don't give the user a chance to quit after each element. Typing 'q' at the prompt causes the program to "go crazy". Typing "1 2 3 4 5" fills the array, with a number of redundant prompts in between. A flaw in Dr. Carter's "read_int", IMHO. Perhaps that's why he calls scanf directly, and jumps through hoops to deal with its insanity, in his array1.asm example.

As soon as you get out of this class, learn to input numbers without depending on that abomination scanf!!!

Speaking of "class"... I hope you guys realize that some schools consider getting *any* help outside of the official University help system to be "cheating". I consider it to be "cooperating to solve a problem", and I consider it to be more valuable than competing to "beat" one another, but academia doesn't see it that way, and they take it very seriously! You've been warned.

Best,
Frank