News:

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

Need help with a program.

Started by Disowned, April 26, 2009, 10:51:56 PM

Previous topic - Next topic

Disowned

Hello, I'm new to this forum. I was writing a program for a class assignment that involved grabbing input from the user, stuffing it into an array like cheap meat in a taco; processing the data/meat to from two arrays and adding them together. The problem I am having is that the program seems to crash whenever I want to read through both arrays at the same time, I'm thinking I might need a third array just to handle the result but that seems kind of trivial. Anyway, here's the code: the problem starts after the "Press any key" prompt.

http://paste2.org/p/192585

Sevag.K

I can see several problems.

line 26, you set ebx to zero.  you then use ebx as an array index while never changing its value (why?)
line 34, you set boxB with the value of ebx no matter what the user inputs.  boxB is always 0.

the big problem comes at line 55 where you set esp (stack pointer!) to zero.  Big no-no, especially if you want to continue using the stack which happens when you call a function (like stdout.put)

there are other problems, but you'll discover them on your own once you fix the esp error and get the program to run.

Disowned

Line 26, ebx is set to zero because; 1. The HLA book actually teaches you to use arrays in this manner, set up register to zero and let the other act as constant. 2. My instruction also taught me to do the same thing.

So yeah.

Line 32: Did not see that, I'll change it now thank you.

Line 55: So that's a stack pointer then, should I use a different register instead? If so, I would like hear you reply. I'll post an update in a bit.


EDIT: Here's the new code, it almost works.

http://paste2.org/p/193404


EDIT: Nevermind, I got it work just by removing somethings you pointed out and by realizing that the 16 and 32 bit version of register allocate memory in virtually the same slots.

Here's the code in case anyone wants to take a look.

http://paste2.org/p/193407

Sevag.K

Quote from: Disowned on April 28, 2009, 12:16:02 AM
Line 26, ebx is set to zero because; 1. The HLA book actually teaches you to use arrays in this manner, set up register to zero and let the other act as constant. 2. My instruction also taught me to do the same thing.

That would make sense if you actually had to use ebx.  In your case, you don't need it at all.

this code: boxA[ebx+esi*4]
can work just as well with: boxA[esi*4]


Quote
So yeah.

Line 32: Did not see that, I'll change it now thank you.

Line 55: So that's a stack pointer then, should I use a different register instead? If so, I would like hear you reply. I'll post an update in a bit.


EDIT: Here's the new code, it almost works.

http://paste2.org/p/193404


EDIT: Nevermind, I got it work just by removing somethings you pointed out and by realizing that the 16 and 32 bit version of register allocate memory in virtually the same slots.

Here's the code in case anyone wants to take a look.

http://paste2.org/p/193407

Yes, you should generally leave the stack pointer alone unless you absolutely don't need the stack.  Actually, leave any register that ends with 'P' alone, since they are important pointers.  ESP, EBP and EIP and should be used only when you are absolutely sure of what you are doing :)

The code is looking better, but if you want to improve it, here are some more pointers.  You are using more registers than you need to be using.  You can do without ESI in the loop that uses CX and ESI.  You can make the loop much more efficient if you just use ecx as the loop counter and address reference.  There are other places where you can reduce the number of used registers.

Nice joke at the end.


Disowned

Thanks, I'll try cleaning up the code after the school semester is over. BTW, I finished the final project for my asm class so I might as post what I was working. It was a program designed to add or multiply numbers from array input.

http://paste2.org/p/195554

I actually manage to split that 200+ code monster into 4 smaller files. Speaking of which I got a question about procedures. You can't call a procedure from within a procedure, can you? I tried adding that into the problem but after having a procedure refer to a procedure I couldn't get the procedure to refer to its parent.

Sevag.K


You can call any procedure that is declared before location where you call from.  If you wish to call a procedure declared further down in your code, you have to declare it @forward, if calling a procedure in another module, you have to declare it @external.

eg:

procedure someProc; @forward;


...

procedure someOtherProc;
begin someOtherProc;
    someProc();  // since someProc is declared @forward, HLA will be able to find it from here.
end someOtherProc;

procedure someProc;
begin someProc;
end someProc;