Jump to content

How do i modify this assembly code in order to do "parameter passing"?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
hands0m3

hands0m3

    Newbie

  • Members
  • Pip
  • 1 posts
This is my main program in C:

--------
#include <stdio.h>

main() {
	int num1 = 0;
	int num2 = 0;
	int sum = 0;
	printf("Enter first number:");
	scanf("%d", &num1);
	printf("Enter second number:");
	scanf("%d", &num2);
	mysum();
	printf("Answer is %d", sum);
}
--------

This is the program containing mysum()
int mysum()
{
	
}
----------

THis is the assembly code of the main program
	.file	"mp.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.section .rdata,"dr"
LC0:
	.ascii "Enter first number:\0"
LC1:
	.ascii "%d\0"
LC2:
	.ascii "Enter second number:\0"
LC3:
	.ascii "Answer is %d\0"
	.text
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$24, %esp
	andl	$-16, %esp
	movl	$0, %eax
	addl	$15, %eax
	addl	$15, %eax
	shrl	$4, %eax
	sall	$4, %eax
	movl	%eax, -16(%ebp)
	movl	-16(%ebp), %eax
	call	__alloca
	call	___main
	movl	$0, -4(%ebp)
	movl	$0, -8(%ebp)
	movl	$0, -12(%ebp)
	movl	$LC0, (%esp)
	call	_printf
	leal	-4(%ebp), %eax
	movl	%eax, 4(%esp)
	movl	$LC1, (%esp)
	call	_scanf
	movl	$LC2, (%esp)
	call	_printf
	leal	-8(%ebp), %eax
	movl	%eax, 4(%esp)
	movl	$LC1, (%esp)
	call	_scanf
	call	_mysum
	movl	-12(%ebp), %eax
	movl	%eax, 4(%esp)
	movl	$LC3, (%esp)
	call	_printf
	leave
	ret
	.def	_mysum;	.scl	2;	.type	32;	.endef
	.def	_scanf;	.scl	2;	.type	32;	.endef
	.def	_printf;	.scl	2;	.type	32;	.endef
-------

And this one if the asm code of mysum()
	.file	"mysum.c"
	.text
.globl _mysum
	.def	_mysum;	.scl	2;	.type	32;	.endef
_mysum:
	pushl	%ebp
	movl	%esp, %ebp
	popl	%ebp
	ret
--------


How do i modify the last code (_mysum) in order to compute for the sum in the main program and assign its value?

Edited by WingedPanther, 11 October 2008 - 01:59 PM.
add code tags (the # button)


#2
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Just modify the C, and recompile.

#3
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
Hi

If you wish to do parameter passing thought assembly there is an example, with a sum function:


[BITS 16]

[ORG  100h]



	SECTION .data

var1:	dw	0

var2:	dw	0

total:	dw	0         ;the variable which will hold the sum of var1 + var2



	SECTION .text

global	_start


_start:

	mov	word [var1], 10

	mov	word [var2], 10         ;var1 and var2 are equal to 10


	push	word [var1]              ;push the value of var1 into the stack

	push	word [var2]              ;push the value of var2 into the stack

	push	word total                ;push the address of total into the stack

	call	near _sum                ;call function sum, which will sum var1 + var2 and pass the result to var 'total'

	

	call	near _exit



_sum:

	push	bp                          ;push the value of base pointer into the stack

	mov	bp, sp                     ;the value of bp is now equal to the value of stack pointer, which will be used to access the stack params


	mov	ax, word [bp+8]        ;ax is equal to the value of var1

	add	ax, word [bp+6]        ;add ax the value of var2 (var1+var2)


	mov	bx, word [bp+4]        ;bx is equal to the address of total

	mov	word [bx], ax            ;the value of total is ax (var1+var2)


	pop	bp                    

	retn	6                            ;mov sp to it's value before calling the function



_exit:

	mov	ah, 00h

	int	21h


This code is written in NASM syntax.