views:

165

answers:

2

I'm having a bit of difficulty converting the following java code into Intel IA-32 Assembly:

 class Person() {
   char name [8];
   int age;

   void printName() {...}

   static void printAdults(Person [] list) {
     for(int k = 0; k < 100; k++){
       if (list[k].age >= 18) {
         list[k].printName();
       }
     }
  }
}

My attempt is:

Person:
    push ebp; save callers ebp
    mov ebp, esp; setup new ebp
    push esi;    esi will hold name
    push ebx;    ebx will hold list
    push ecx;    ecx will hold k

init:
    mov esi, [ebp + 8];
    mov ebx, [ebp + 12];
    mov ecx, 0;    k=0

forloop:
    cmp ecx, 100;
    jge end;    if k>= 100 then break forloop 
    cmp [ebx + 4 * ecx], 18 ;
    jl auxloop;    if list[k].age < 18 then go to auxloop
    jmp printName;

printName:


auxloop:
    inc ecx;
    jmp forloop;

end:
    pop ecx;
    pop ebx;
    pop esi;
    pop ebp;

Is my code correct?

NOTE: I'm not allowed to use global variables.

+2  A: 

You have it a little wrong, first, you are coding it like you are getting a name and an age as parameters, you are not, you just need ebx to hold the address to the list. And you have your pointer math a little off, assuming chars are 1 byte (thus 8 bytes in your array), integers and pointers 4 bytes this may work :

Person:
    push ebp; save callers ebp
    mov ebp, esp; setup new ebp

init:
    mov ebx, [ebp + 4];
    mov ecx, 0;    k=0

forloop:
    cmp ecx, 100;
    jge end;    if k>= 100 then break forloop 
    cmp [ebx + 8 + 12 * ecx], 18 ; 12 * ecx to advance 12 bytes (8 char 4 int), plus 8 to compare the integer, which is 8 bytes away from the start of the pointer.
    jl auxloop;    if list[k].age < 18 then go to auxloop
    jmp printName;

printName:


auxloop:
    inc ecx;
    jmp forloop;

end:
    pop ebp;
Francisco Soto
Many thanks Francisco, if i could possibly ask you a quick question: "list" is being used as an input parameter in printAdults, why isn't this being pushed?
Kay
+1  A: 

here is my version, should be a little faster(with stack frame pointer, could probably be removed :P)

_start:
    PUSH EBP
    MOV EBP,ESP
    PUSH EDI
    PUSH ESI
    MOV ESI, [EBP + 8]      ; __stdcall assumed -> get arg: list
    MOV EDI, 100        ; i=100 -> iterations of the loop

_loop:
    CMP [ESI + 8], 18       ; pPerson->age >= 18
    JL _next
    MOV ECX,ESI
    CALL printName      ; __thiscall assumed

_next:
    ADD ESI,12          ; list += sizeof(person)
    SUB EDI,1                    ; i--
    JNZ _loop

_end:
    POP ESI
    POP EDI
    POP EBP
    RETN 4
Necrolis