Intel Assembly Programming
- by Kay
class MyString{
char buf[100];
int len;
boolean append(MyString str){
int k;
if(this.len + str.len>100){
for(k=0; k<str.len; k++){
this.buf[this.len] = str.buf[k];
this.len ++;
}
return false;
}
return true;
}
}
Does the above translate to:
start:
push ebp ; save calling ebp
mov ebp, esp ; setup new ebp
push esi ;
push ebx ;
mov esi, [ebp + 8] ; esi = 'this'
mov ebx, [ebp + 14] ; ebx = str
mov ecx, 0 ; k=0
mov edx, [esi + 200] ; edx = this.len
append:
cmp edx + [ebx + 200], 100
jle ret_true ; if (this.len + str.len)<= 100 then ret_true
cmp ecx, edx
jge ret_false ; if k >= str.len then ret_false
mov [esi + edx], [ebx + 2*ecx] ; this.buf[this.len] = str.buf[k]
inc edx ; this.len++
aux:
inc ecx ; k++
jmp append
ret_true:
pop ebx ; restore ebx
pop esi ; restore esi
pop ebp ; restore ebp
ret true
ret_false:
pop ebx ; restore ebx
pop esi ; restore esi
pop ebp ; restore ebp
ret false
My greatest difficulty here is figuring out what to push onto the stack and the math for pointers.
NOTE:
I'm not allowed to use global variables and i must assume 32-bit ints, 16-bit chars and 8-bit booleans.