Intel IA-32 Build

I'm having difficulty converting the following Java code to 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:

 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 am not allowed to use global variables.

+4
source share
2 answers

This is a bit wrong with you, first of all, you encode it as if you get the name and age as parameters, and you don’t, you just need ebx to keep the address in the list. And you have slightly fewer math indications, assuming the characters are 1 byte (thus 8 bytes in your array), integers and pointers 4 bytes that might 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; 
+2
source

here is my version, it should be a little faster (with a stack frame pointer, you can probably delete it: 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 
+1
source

Source: https://habr.com/ru/post/1308224/


All Articles