Reputation: 845
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.
Upvotes: 0
Views: 383
Reputation: 192657
Sounds like you have an assignement, to create Intel x86 assembly code that performs a string append operation.
If that's true, It might be instructive for you to examine the disassembled output of a C compiler - like the Intel C compiler, or the Microsoft VC++ Express compiler. Compile some really simple C code, then disassemble and look at the results.
Compilers usually do things a little differently than a human coder would, but in my experience it's not difficult to figure out what's happening in the generated code. You can see how the ASM code manages the stack, how pointer arithmetic in done, how and when registers are initialized, which registers are used and how, and so on.
A simple C routine might be, your own version of strlen.
Then, add complexity or change things, compile and disassemble again, and see what happens. Like, instead of strlen, make a C routine that returns the last char in a string.
or, if you just want the answer, you could try Codecodex. :)
Upvotes: 3