English 中文(简体)
Translate a FOR to assembler
原标题:

I need to translate what is commented within the method, to assembler. I have a roughly idea, but can t.

Anyone can help me please? Is for an Intel x32 architecture:

int 
secuencia ( int n, EXPRESION * * o )
{
  int a, i;
//--- Translate from here ...
  for ( i = 0; i < n; i++ ){
    a = evaluarExpresion( *o );
    o++;
  }
  return a ;
//--- ... until here.
}

Translated code must be within __asm as:

__asm {
        translated code
}

Thank you,

FINAL UPDATE:

This is the final version, working and commented, thanks to all for your help :)

int
secuencia ( int n, EXPRESION * * o )
{
    int a = 0, i;
    __asm
    {
        mov dword ptr [i],0             ; int i = 0
        jmp salto1
        ciclo1:
        mov eax,dword ptr [i]           
        add eax,1                       ; increment in 1 the value of i
        mov dword ptr [i],eax           ; i++
        salto1: 
        mov eax,dword ptr [i]           
        cmp eax,dword ptr [n]           ; Compare i and n
        jge final                       ; If is greater goes to  final 
        mov eax,dword ptr [o] 
        mov ecx,dword ptr [eax]         ; Recover * o (its value)
        push ecx                        ; Make push of * o (At the stack, its value)
        call evaluarExpresion           ; call evaluarExpresion( * o )
        add esp,4                       ; Recover memory from the stack (4KB corresponding to the * o pointer)
        mov dword ptr [a],eax           ; Save the result of evaluarExpresion as the value of a
        mov eax,dword ptr [o]           ; extract the pointer to o
        add eax,4                       ; increment the pointer by a factor of 4 (next of the actual pointed by *o)
        mov dword ptr [o],eax           ; o++
        jmp ciclo1                      ; repeat
        final:                          ; for s final
        mov eax,dword ptr [a]           ; return a - it save the return value at the eax registry (by convention this is where the result must be stored)
    }
}
最佳回答

Essentially in assembly languages, strictly speaking there isn t a notion of a loop the same way there would be in a higher level language. It s all implemented with jumps (eg. as a "goto"...)

That said, x86 has some instructions with the assumption that you ll be writing "loops", implicitly using the register ECX as a loop counter.

Some examples:

    mov ecx, 5         ; ecx = 5
.label:
    ; Loop body code goes here
    ; ECX will start out as 5, then 4, then 3, then 1...
    loop .label        ; if (--ecx) goto .label;

Or:

    jecxz .loop_end    ; if (!ecx) goto .loop_end;
.loop_start:
    ; Loop body goes here
    loop .loop_start   ; if (--ecx) goto .loop_start;
.loop_end:

And, if you don t like this loop instruction thing counting backwards... You can write something like:

    xor ecx, ecx       ; ecx = 0
.loop_start:
    cmp ecx, 5         ; do (ecx-5) discarding result, then set FLAGS
    jz .loop_end       ; if (ecx-5) was zero (eg. ecx == 5), jump to .loop_end
    ; Loop body goes here.
    inc ecx            ; ecx++
    jmp .loop_start
.loop_end:

This would be closer to the typical for (int i=0; i<5; ++i) { }

问题回答

Note that

for (init; cond; advance) {
    ...
}

is essentially syntactic sugar for

init;
while(cond) {
   ...
   advance;
}

which should be easy enough to translate into assembly language if you ve been paying any attention in class.

Use gcc to generate the assembly code

gcc -S -c sample.c

man gcc is your friend

For that you would probably use the loop instruction that decrements the ecx (often called, extended counter) at each loop and goes out when ecx reaches zero.But why use inline asm for it anyway? I m pretty sure something as simple as that will be optimized correctly by the compiler...

(We say x86 architecture, because it s based on 80x86 computers, but it s an "ok" mistake =p)





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签