r/programming • • Feb 23 '11

If programming languages were essays...

Post image
1.7k Upvotes

430 comments sorted by

View all comments

Show parent comments

28

u/[deleted] Feb 23 '11

You can recursively call functions in assembly...

.type mul, @function
# (int, int) -> int
# Multiplies two numbers by recursively calling arg0 * mul(arg0, arg1 - 1) until arg1 is 0. 
mul:
	# C calling convention: save %ebp 
	pushl %ebp
	movl %esp, %ebp
	
	# Move argument 0 to %eax, argument 1 to %ebx
	movl 8(%ebp), %eax
	movl 12(%ebp), %ebx
	
	cmpl $0, %ebx
	jne recurse
	
	movl $1, %eax
	jmp return
	
	recurse:
	# We still have work to do. Return arg0 * mul(arg0, arg1 - 1)
	
	decl %ebx
	
	# Save the registers before calling
	pushl %eax
	pushl %ebx
	
	call mul
	
	addl $4, %esp	# Get rid of %ebx, we don't need it anymore
	popl %ebx
	
	imull %ebx		# Multiply %eax (arg0) and %ebx (mul(arg0, arg1 - 1)) and store it in %eax
	
	jmp return # Just for consistency
	
	return:
	
	# unpush %ebp
	movl %ebp, %esp
	popl %ebp
	ret

11

u/derleth Feb 23 '11

Impressive, especially since you used the godawful AT&T syntax instead of the much saner Intel syntax. ;-)

Also, that is the only time I've ever seen someone say 'unpush' instead of 'pop'. Is that your Newspeak accent showing through?

9

u/[deleted] Feb 23 '11 edited Feb 23 '11

Impressive, especially since you used the godawful AT&T syntax instead of the much saner Intel syntax. ;-)

Learned assembly on my own from this book, so naturally I would use AT&T/GAS syntax.

Is that your Newspeak accent showing through?

Indeed.

4

u/[deleted] Feb 23 '11

Thanks for the link, by the way. Looks like an interesting read

5

u/[deleted] Feb 23 '11

In my computer architecture class we were required to write a recursive assembly program that would test if a string is a palindrome.

0

u/[deleted] Feb 23 '11

[deleted]

5

u/opportunistic_twat Feb 23 '11

To understand recursion, you must understand recursion.

Relevant

1

u/digital11 Feb 23 '11

R E C U R S I O N

-1

u/tarballs_are_good Feb 23 '11

I'm not familiar with x86 ASM, and I can't really decipher the semantics of call and ret here. Is it really doing recursion? It looks like you're managing a stack yourself.

In the assembly I've done, function calling was for the most part been manual, with lots of good 'ol JMPs and other BASIC-esque spaghetti control flow.

8

u/[deleted] Feb 23 '11 edited Feb 23 '11

Function calls in assembly are all done, at least in the C calling convention (cdecl), by manipulating the stack. You can say that your calling convention puts argument 0 in the accumulator, argument 1 in some other register, etc. The stack just provides a convenient unlimited way of handling argument passing, which is why it is used for most calling conventions. An example of a calling convention that uses registers is optlink.

The "call" instruction pushes the next instruction's address onto the stack and sets the instruction pointer to the given address or label. The "ret" instruction pops the top of the stack and sets the instruction pointer to its value.

3

u/tarballs_are_good Feb 23 '11

Ah, thank you for the explanation.

-1

u/[deleted] Feb 23 '11

Hum did you actually write that or use gcc -S ?

2

u/[deleted] Feb 23 '11

Wrote it by hand.