Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > how to deal with FOR
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 31st October 20:53
fiw0000
External User
 
Posts: 1
Default how to deal with FOR



In the source language, there is for statement and the rule is like
the following:

for i := 1 to 9 by 2 do statement

when I generate IA-32 code for it, how could I deal with the 9 and 2?
I think I should put them on the stack like i, (I treat the IA-32
assembler as a stack machine) how could I know the address of them in
their future use, for example, to compare whether i > 9. They are not
like i, which is the variable of the source language.

Thank you.
  Reply With Quote


 


2 31st October 20:54
nudge
External User
 
Posts: 1
Default how to deal with FOR



In your example, 2 and 9 are constants, not variables. They do not
live on the stack, and they do not have addresses.

The code would look like this:

mov ecx, 1 ; initialize induction variable i
jmp L2
L1: statement ; whatever
add ecx, 2
L2: cmp ecx, 9
jle L1

In this case, you can remove the unconditional jump, since you know
the loop will be executed at least one time.
  Reply With Quote
3 31st October 20:54
arargh312nospam
External User
 
Posts: 1
Default how to deal with FOR


Since they are constants, you don't need to do anything with them,
except use them in instructions:

1 FOR I = 1 TO 9 STEP 2
L00001: mov ax,0001h ** load initial value
jmp I00003 ** goto to exit check

2 A = A + 1' do statememt
I00004:
L00002: inc A%

3 NEXT I
L00003: mov ax,I% ** increment after statement
add ax,0002h
I00003: mov I%,ax ** save I
cmp ax,0009h ** check for done
jng I00004 ** loop again


It gets tricky when everything is a variable :-)

5 FOR I = A1 TO A9 STEP A2
L00005: mov ax,A9%
mov t000C%,ax ** save ending value
mov ax,A2%
mov t0010%,ax ** save step value
mov ax,A1% ** set initial value
jmp I00005

6 A = A + 1' do statememt
I00006:
L00006: inc A%

7 NEXT I
L00007: mov ax,t0010% ** increment after statement
add ax,I%

I00005: mov I%,ax ** save I
(based of sign of step var, see if you are done)
cmp word ptr t0010%,00h
jnl I00007
cmp ax,t000C%
jnl I00006
jmp I00008
I00007: cmp ax,t000C%
jng I00006 --
Arargh312 at [drop the 'http://www.' from ->] http://www.arargh.com
BCET Basic Compiler Page: http://www.arargh.com/basic/index.html

To reply by email, remove the garbage from the reply address.
  Reply With Quote
4 31st October 20:54
randall hyde
External User
 
Posts: 1
Default how to deal with FOR


You should take a look at the chapter on implementing
low-level control structures in "The Art of Assembly Language"
(http://www.nostarch.com and http://webster.cs.ucr.edu).
It will tell you how to convert most high-level control structures
into low-level machine code.
Cheers,
Randy Hyde
  Reply With Quote
5 31st October 20:55
fiw0000
External User
 
Posts: 1
Default how to deal with FOR


Thank you all.

But 9 and 2 are variables here.

Because I am construting a compiler for this simple language. loop end
value and step value will change according to the source language. I
am not just changing the special case to assembly language.
  Reply With Quote
6 31st October 20:56
lavron
External User
 
Posts: 1
Default how to deal with FOR


Rosa,

Is there any reason why you cannot allocate your own two variables,
name them according to your own internal convention (using syntax
acceptable to your assembler but not to the high level language you
are compiling), store 9 and 2 in them, respectively, and leave these
values intact? Aharon
  Reply With Quote
7 31st October 20:56
nudge
External User
 
Posts: 1
Default how to deal with FOR


for i := MIN to MAX by STEP do statement

Assume MIN, MAX, and STEP are global variables, the above code
sequence would translate to:

MOV ECX, [MIN] ; initialize induction variable i
JMP L2
L1:
STATEMENT
ADD ECX, [STEP]
L2:
CMP ECX, [MAX]
JLE L1

If MIN, MAX, STEP live on the stack, you would use EBP or ESP to
reference them.

I must be missing something...
  Reply With Quote
8 10th November 05:51
fiw0000
External User
 
Posts: 1
Default how to deal with FOR


In the source language reference manual,
it says that
for expression1 := expresson2 to expression3 [by expression4] do
statement1

9 and 2 are all expressions , we can't expect the user to write a :=
9 and b :=2 , for i:=0 to a by b do ...

So I have to find a way to allocate address for expression3 and
expression4 and also find a way to know where they are when I use them
to judge whether to continue the loop.
  Reply With Quote
9 10th November 05:51
tim roberts
External User
 
Posts: 1
Default how to deal with FOR


Your compiler has to be able to handle temporaries. You're dividing the
code into basic blocks and building a directed graph, right? You know at
any time how many temporaries you're tracking. When you start to generate
assembler code (which you won't do until you parse the graph later), you'll
keep the temporaries in registers until you run out of free registers, then
you'll start dumping them to memory on the stack.

Are you using yacc or ANTLR to generate your compiler?
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
  Reply With Quote
10 10th November 05:51
bjarni juliusson
External User
 
Posts: 1
Default how to deal with FOR


So it's a register allocation problem? Read about graph coloring.
There's an explanation of it in the Dragon Book, but I think the Tiger
Book by Andrew Appel might have a better description. It's a matter of
creating code that evaluates expressions into virtual registers, and
then figuring out which actual registers are available at any given time
and chosing which values to spill onto the stack and so forth. But you
said that you pretend the processor is a stack machine - still, the
result of the expression in the test doesn't need to be stored between
the iterations, does it? It's recalculated each time round. Or is there
something with this language that isn't obvious that we should know?

Or am I missing something...?


--
Remove the furry animal from my address to send email:

bjarni.ferret@update.ferret.uu.se
  Reply With Quote
Reply


Thread Tools
Display Modes




666