News:

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

problem with looping

Started by boon, October 02, 2008, 07:29:49 AM

Previous topic - Next topic

boon

if i have many identifiers and i want to print it out at different time, what can i do with it? below is my example..

.data
second   dd   0
pic1       db   "hello",0
pic2       db   "world",0
pic3       db   "welcome",0
pic4       db   "to",0
pic5       db   "assembly language",0

.code
.WHILE second <=5

print ADDR pic[second+1]

.ENDW
inc second

it runs error, can anyone help me modify my code?

jj2007

First of all, it is not a good idea to increase a loop pointer outside the loop. But there are quite a number of other little quirks - perhaps it would be a good idea to read some basic tutorials about pointers etc.

include \masm32\include\masm32rt.inc

.data
second   dd   0
pic1       db   "hello ", 0
pic2       db   "world ", 0
pic3       db   "welcome ", 0
pic4       db   "to ", 0
pic5       db   "assembly language", 0
pointers dd pic1, pic2, pic3, pic4, pic5

.code
start:

.WHILE second<5
mov eax, second
print [pointers+4*eax]
inc second
.ENDW

invoke ExitProcess, 0

end start

Vortex

boon,

Assuming that your code is depending on the print macro, you can easily calculate the offset of the strings :


include \masm32\include\masm32rt.inc

.data
pic1    db   "hello ", 0
pic2    db   "world ", 0
pic3    db   "welcome ", 0
pic4    db   "to ", 0
pic5    db   "assembly language", 0

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC uses esi ebx

    mov     esi,OFFSET pic1
    mov     ebx,5
@@:
    print   esi
    lea     esi,[esi+eax+1]     ; StdOut called from the print macro
    dec     ebx                 ; returns the length of the item
    jnz     @b                  ; to be displayed
    ret

main ENDP

END start


I would recommend jj2007's method, defining a set of pointers to the strings. It's more precise.

herge

 Hi boon:

You can also rearrange the data and not use a loop
like this.


include \masm32\include\masm32rt.inc

.data
second   dd   0
pic1       db   "hello world welcome to assembly language", 13, 10, 0

.code
start:
lea edx, offset pic1
print edx
     inkey
     invoke ExitProcess, 0
end start



inkey is a macro that waits for keyboard input,
use it so you can see your output other
wise windows runs your program and closes
it before you can see it!
13 is CR or carriage return also known as enter
10 is LF or line feed
Also if you have a debugger trace thru your program.
Make sure pushs equal pops if not something
horrible is bound to happen. usually a c5 error access
violation or message to Microsoft.

Regards herge

// Herge born  Brussels, Belgium May 22, 1907
// Died March 3, 1983
// Cartoonist of Tintin and Snowy

boon

Thanks for your reply jj2007, but something i not sure here,

Quote from: jj2007 on October 02, 2008, 08:19:31 AM
print [pointers+4*eax]


actually what is going on here? why the pointers has to add 4 and multiply by eax??

boon

Hi herge,

actually what i want to do is i would like to print a sentence at a different time such as at 1st second i print "hello" then at 2nd second i print "world", not to print the sentences at a same time. However, thanks for your reply. :bg

But now another problem is if i want to use print ADDR, it will run error, why??
if now my code become
.code
pic1    db   "hello",cr,lf
         db    "world",0

pic2    db   "welcome",cr,lf
         db   "to",cr,lf
         db  "my world",0

pic      db  "assembly",cr,lf
          db  "language",0

jj2007

Quote from: boon on October 02, 2008, 11:37:06 AM
Thanks for your reply jj2007, but something i not sure here,

Quote from: jj2007 on October 02, 2008, 08:19:31 AM
print [pointers+4*eax]


actually what is going on here? why the pointers has to add 4 and multiply by eax??

pointers dd pic1, pic2, pic3, pic4, pic5

means
insert the offset of pic1 into the dword at address pointers
insert the offset of pic2 into the dword at address pointers+4
insert the offset of pic3 into the dword at address pointers+8 etc

print [pointers+4*eax] means take out the dword at address pointers+4*eax, where eax is your counter.
And again, take the time to read a very basic introduction to assembler. This forum is not the place for asking questions at that level. Members here assume that you read the book before asking questions.

Mirno

Hi Boon,

The problem with your original code is that you are trying to reference one memory address using the contents of another memory location. The x86 doesn't offer such an instruction, so it's impossible to do in a single step.
Secondly you've misunderstood the masm syntax, in that:

  mov eax, second ; avoid dereferencing via a memory location
  print ADDR pic[eax+1]

will not do what you think it will. The argument you're passing to print is the address of "pic" plus "second" + 1 bytes, not referencing the picn variable.

That jj2007 is suggesting is that you have your strings, and then put their addresses in an array (which called "pointers"). Note that the addresses contained within this array are all 4 bytes long - this is why eax is multiplied by 4.
Unlike higher level languages, the array operator moves the pointer along "n bytes" every time, not "n * sizeof type".

Mirno