Reputation: 3
I need some help deciphering what this means, I put comments of what I think it does but I'm not entirely sure.
movl 12(%ebp), %eax //variable (x) moves into eax register
addl $4, %eax // add the value 4 to x
movl (%eax), %eax //eax = *x;
movl %eax, (%esp) //stack pointer = *x
call strlen //calls to gets length of string
movl %eax, %edx //copy *x to edx register, name it *y
movl %edx, %eax //copy *y to eax
sall $2, %eax //shift *x left 2 (eax)
addl %edx, %eax //*x + *y = *x (x is shifted 2, remember)
movl %eax, (%esp) // move the new *x string onto stack pointer
call malloc // memory allocate for the string
movl %eax, 28(%esp) //move this string onto a new variable, lets say z
movl $.LC1, %edx //move string in LC1 into edx
movl 12(%ebp), %eax //repeat what was at the top
addl $4, %eax
movl (%eax), %eax
movl 28(%esp), %ecx // move z into ecx register
movl %ecx, 8(%esp) // move z closer to stack pointer
movl %edx, 4(%esp) // move y closest to stack pointer
movl %eax, (%esp) // set stack pointer to x. now the stack goes: x, y, z
call __isoc99_sscanf //return the number of input items successfully matched
cmpl $1, %eax //if x == 1,
je .L20 //jump to L20
movl $.LC2, (%esp) //else, LC2 becomes stack pointer
call puts //calls procedure, which is LC2
movl $1, %eax // makes x = 1
jmp .L19 // jump to end
Upvotes: 0
Views: 738
Reputation: 28921
It's not quite correct. Here's my attempt:
movl 12(%ebp), %eax //[esp] = [[ebp+12]+4]
addl $4, %eax
movl (%eax), %eax
movl %eax, (%esp)
call strlen //eax = length of string
movl %eax, %edx //edx = length of string
movl %edx, %eax //useless instruction
sall $2, %eax //eax = length x 4
addl %edx, %eax //eax = length x 5
movl %eax, (%esp) //allocate length x 5 bytes
call malloc
movl %eax, 28(%esp) //[ebp+28] = ptr to allocated memory
movl $.LC1, %edx //edx = offset .LC1
movl 12(%ebp), %eax //eax = [[ebp+12]+4]
addl $4, %eax
movl (%eax), %eax
movl 28(%esp), %ecx //[esp+8] = ptr to allocated memory
movl %ecx, 8(%esp)
movl %edx, 4(%esp) //[esp+4] = offset .LC1
movl %eax, (%esp) //[esp] = [[ebp+12]+4]
call __isoc99_sscanf //return the number of input items successfully matched
cmpl $1, %eax //if result == 1
je .L20 //jump to L20
movl $.LC2, (%esp) //[esp] = offset .LC2
call puts //display .LC2 string
movl $1, %eax //eax = 1
jmp .L19 //jump to ???
Upvotes: 1
Reputation: 57784
It looks right as far as I looked. The third instruction would be less confusing if it kept only one paradigm;
//x = *x;
Also, this one might be a misunderstanding:
movl %eax, (%esp) //stack pointer = *x
call strlen //calls to gets length of string
movl %eax, %edx //copy *x to edx register, name it *y
This is not changing the stack pointer, only the word at the top of stack. The stack pointer remains pointing to the same place. It calls strlen which expects its parameter to be on the top of stack. strlen()
returns its value in %eax, as is the custom with all functions. So these might be better rewritten as
movl %eax, (%esp) // p1 = x (pointer to string)
call strlen // length of string
movl %eax, %edx // copy strlen(x) to edx register, name it y
Perhaps you can use that much to figure out more on your own.
Upvotes: 0