# Arithmetic shift confusion in assembly.

Discussion in 'Mac Programming' started by SAEinSDSU, Mar 24, 2010.

1. ### SAEinSDSU macrumors newbie

Joined:
Feb 17, 2010
#1
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.

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

the assembly equivalent is

1 movl 8(%ebp), %edx
3 movl -16(%ebp), %eax
4 sall \$2, %eax
6 movl -16(%ebp), %eax
7 sall \$2, %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

2. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#2
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

3. ### kpua macrumors 6502

Joined:
Jul 25, 2006
#3
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.

4. ### SAEinSDSU thread starter macrumors newbie

Joined:
Feb 17, 2010
#4
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
movl (%eax), %eax
movl %eax, 16(%ebp)
jmp whilebegin
whilebody:
movl %ebx, %edx
movl %edi, %eax
sall \$2, %eax
movl %edi, %eax
sall \$2, %eax
movl (%eax), %eax
movl %eax, (%edx)
decl %edi
whilebegin:
cmpl \$0, %edi
js end
movl %edi, %eax
sall \$2, %eax
movl (%eax), %eax
cmpl 16(%ebp), %eax
jg whilebody
end:
movl %ebx, %edx
movl %edi, %eax
sall \$2, %eax
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.

5. ### gnasher729 Suspended

Joined:
Nov 25, 2005
#5
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.

6. ### Sydde macrumors 68020

Joined:
Aug 17, 2009
#6
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?

7. ### SAEinSDSU thread starter macrumors newbie

Joined:
Feb 17, 2010
#7
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....

8. ### gnasher729 Suspended

Joined:
Nov 25, 2005
#8
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?

9. ### Sydde macrumors 68020

Joined:
Aug 17, 2009
#9
Well, given what you are doing, that is the result I would expect. See what happens if you run the process right-to-left (with descending indexes).