Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SAEinSDSU

macrumors newbie
Original poster
Feb 17, 2010
12
0
Hello all, Quick question about syntax and what exactly this code is doing...

So I understand that the arithmetic shift command multiplies or divides by 2^n, depending on the n. However I am confused on some assembly( i386 architecture for 32 bit )coding. here is a part of the code that I am confused about.

In C it reads....
arr[j+1] = arr[j];

the assembly equivalent is

1 movl 8(%ebp), %edx
2 addl $4, %edx
3 movl -16(%ebp), %eax
4 sall $2, %eax
5 addl %eax, %edx
6 movl -16(%ebp), %eax
7 sall $2, %eax
8 addl 8(%ebp), %eax
9 movl (%eax), %eax
10 movl %eax, (%edx)


my confusion comes from line 4....what exactly does that mean...n is equal to 2 and divides eax by 4?...OR maybe I am confusing the use of sal in this situation and it does something completely different (probably whats happening) but in either case if someone could clear this up for me it would be very much appreciated....Thanks so much
 
I am really poor with assembly, so i won't try to decipher the whole block, but:
sall $2, %eax

Is an arithmetic shift left by 2 bits, effectively multiplying the value in eax by 4.

-Lee
 
Remember that pointer arithmetic/array indexing isn't just adding a value to the pointer value. You have to account for the size of the type pointed to. I'm assuming your array consists of something 4 bytes wide. Hence line 2 adds 4 (the +1 in j+1) to %edx (arr), and lines 4 and 7 multiply %eax (j) by 4 -- the assembly needs to address at the byte level. Without the multiplication factor, it would address in the middle of one of the 4 byte elements of the array.
 
Thanks kupa...I actually figured that out after a while of picking my brain apart...and came up with this code.....unfortunately it doesn't seem to work and still give me "EXE_BAD_ACCESS" when attempted to compile in Xcode. Its an insertsort function...if anyone would like to attempt to debug for me much appreciated...couldn't find one on google for x86.

.globl _insertionSortA
_insertionSortA:

pushl %ebp
movl %esp, %ebp
movl $1, %esi
movl 8(%ebp), %ebx
movl 12(%ebp), %ecx
jmp cmplength
start:
movl %esi, %eax
decl %eax
movl %eax, %edi
movl %esi, %eax
sall $2, %eax
addl %ebx, %eax
movl (%eax), %eax
movl %eax, 16(%ebp)
jmp whilebegin
whilebody:
movl %ebx, %edx
addl $4, %edx
movl %edi, %eax
sall $2, %eax
addl %eax, %edx
movl %edi, %eax
sall $2, %eax
addl %ebx, %eax
movl (%eax), %eax
movl %eax, (%edx)
decl %edi
whilebegin:
cmpl $0, %edi
js end
movl %edi, %eax
sall $2, %eax
addl %ebx, %eax
movl (%eax), %eax
cmpl 16(%ebp), %eax
jg whilebody
end:
movl %ebx, %edx
addl $4, %edx
movl %edi, %eax
sall $2, %eax
addl %eax, %edx
movl 16(%ebp), %eax
movl %eax, (%edx)
incl %esi
cmplength:
movl %esi, %eax
cmpl %ecx, %eax
jl start
ret

Thank you if you would like to look it over.
 
I looked over it, but I'm not a masochist, so I left it at the looking.

Hint: Take a look at the indexing address modes. Much easier to write an address as 4(%ebx,4*%eax) or whatever the syntax is than having half a dozen adds, shifts etc.

Hint 2: In XCode, switch to mixed code/assembler view, then you can step through your assembler code as well in the debugger.
 
Is this returning to %ebp? perhaps I am unclear on the AL concept, but why am I not seeing the popl %ebp that should balance the pushl at the beginning?
 
I am just confused on how to write the line

arr[j+1] = arr[j]; still.

if using the array technique in assembly it would look something like....

movl 4(%ebx, %edi, 4), %eax
movl %eax, (%ebx, %edi, 4)


right?...or no...it doesnt seem to work in my program...i keep getting an output of /bin/bin/bin/bin/bin/bin/bin/bin....
 
I am just confused on how to write the line

arr[j+1] = arr[j]; still.

if using the array technique in assembly it would look something like....

movl 4(%ebx, %edi, 4), %eax
movl %eax, (%ebx, %edi, 4)


right?...or no...it doesnt seem to work in my program...i keep getting an output of /bin/bin/bin/bin/bin/bin/bin/bin....

Have a look what the compiler does. Write a little C program, then Build->Show Assembly Code. And it should be obvious that your two lines will not move 32 bits from arr [j] to arr [j+1]. Read the two lines again. Now imagine very very hard that not you wrote them, but some guy who you hate and you are paid ten bucks for each mistake that you find. So what do these two lines do now?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.