Hi
I tried to pass a pointer value to an assembly procedure. But I don't know how to get the value pointed by the pointer.
Code:
C
--
extern void Sum(int,int *);
int main()
{
int a = 10,b = 15;
Sum(a,&b);
printf("%d",b); //I expect the asm proc to make b = b + a using pointers
return 0;
}
ASM
----
_Sum:
pop ebp
mov ebp,esp
mov ax,[ebp+4] ; Getting my first arg (i.e.) 'a'
mov bx,[ebp+8] ; I don't what am I getting - is it the pointer address ?
; or is it the value of *b
; And what should I do to get the 15+10 stored in b without returning but by ; using the pointer
push ebp
ret
--modshah
How to get pointer values in ASM
RE:How to get pointer values in ASM
Sorry I wrongly typed the asm code. It was a
<push ebp>
and
<pop ebp>
Thanks
modshah
<push ebp>
and
<pop ebp>
Thanks
modshah
RE:How to get pointer values in ASM
mov bx,[ebp+8] ; bx = address of b
mov ax,[bx] ; ax = value of b
just use the register with the address like a variable's name and you'll get its contents
greetings,
Daniel Raffler
mov ax,[bx] ; ax = value of b
just use the register with the address like a variable's name and you'll get its contents
greetings,
Daniel Raffler
RE:How to get pointer values in ASM
Thanks Daniel. But could you please tell me how to do the following C code in assembly.
void sum(int a,int *b)
{
*b = *b + a;
//That is I want to get the value of b - ie [bx] as you told me
// But is it the same as saying
//
// mov bx, [bp+4]
// add ax, [bx]
//
// mov [bx],ax - Will this work ?? for *b = *b + a
}
Also lets say I want to pass a string or char array. How to handle that in assembly ?
modshah
void sum(int a,int *b)
{
*b = *b + a;
//That is I want to get the value of b - ie [bx] as you told me
// But is it the same as saying
//
// mov bx, [bp+4]
// add ax, [bx]
//
// mov [bx],ax - Will this work ?? for *b = *b + a
}
Also lets say I want to pass a string or char array. How to handle that in assembly ?
modshah
RE:How to get pointer values in ASM
Any compiler with an option to produce assembly-language output
will _show_ you how to do something in assembly. For GCC and
Borland C, this option is "-S"
C:\TMP>type foo.c
void sum(int a, int *b) { *b = *b + a; }
C:\TMP>gcc -S -O2 -Wall -W foo.c
C:\TMP>type foo.s
.file "foo.c"
gcc2_compiled.:
___gnu_compiled_c:
.text
.p2align 2
.globl _sum
_sum:
pushl %ebp
movl %esp,%ebp
movl 12(%ebp),%edx
movl (%edx),%eax
addl 8(%ebp),%eax
movl %eax,(%edx)
movl %ebp,%esp
popl %ebp
ret
C:\TMP>gcc -S -O2 -Wall -W -fomit-frame-pointer foo.c
C:\TMP>type foo.s
.file "foo.c"
gcc2_compiled.:
___gnu_compiled_c:
.text
.p2align 2
.globl _sum
_sum:
movl 8(%esp),%edx
movl (%edx),%eax
addl 4(%esp),%eax
movl %eax,(%edx)
ret
C:\TMP>bcc32 -S -O2 -w foo.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
foo.c:
C:\TMP>type foo.asm
{snip debug info and TASM32 stuff}
_sum proc near
push ebp
mov ebp,esp
mov eax,dword ptr [ebp+8]
mov edx,dword ptr [ebp+12]
add dword ptr [edx],eax
pop ebp
ret
will _show_ you how to do something in assembly. For GCC and
Borland C, this option is "-S"
C:\TMP>type foo.c
void sum(int a, int *b) { *b = *b + a; }
C:\TMP>gcc -S -O2 -Wall -W foo.c
C:\TMP>type foo.s
.file "foo.c"
gcc2_compiled.:
___gnu_compiled_c:
.text
.p2align 2
.globl _sum
_sum:
pushl %ebp
movl %esp,%ebp
movl 12(%ebp),%edx
movl (%edx),%eax
addl 8(%ebp),%eax
movl %eax,(%edx)
movl %ebp,%esp
popl %ebp
ret
C:\TMP>gcc -S -O2 -Wall -W -fomit-frame-pointer foo.c
C:\TMP>type foo.s
.file "foo.c"
gcc2_compiled.:
___gnu_compiled_c:
.text
.p2align 2
.globl _sum
_sum:
movl 8(%esp),%edx
movl (%edx),%eax
addl 4(%esp),%eax
movl %eax,(%edx)
ret
C:\TMP>bcc32 -S -O2 -w foo.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
foo.c:
C:\TMP>type foo.asm
{snip debug info and TASM32 stuff}
_sum proc near
push ebp
mov ebp,esp
mov eax,dword ptr [ebp+8]
mov edx,dword ptr [ebp+12]
add dword ptr [edx],eax
pop ebp
ret
RE:How to get pointer values in ASM
Thanks chris I figured a way out.
//
// *b = *b + a
// using pointers
//
// C Code
// CallSum.c
#include<stdio.h>
int main()
{
int a=10,b=15;
Sum(a,&b);
printf("%d\n",b);
}
// ASM Code
// Sum.asm
global _Sum
SEGMENT .text
[BITS 32]
_Sum:
push ebp
mov ebp,esp
mov eax,[ebp+8]
mov ebx,[ebp+12] ;The BYTE PTR didn't work
add eax,[ebx] ;some one told me PTR is not supported in NASM
mov [ebx],eax ;Is that true ?
pop ebp
ret
//
// *b = *b + a
// using pointers
//
// C Code
// CallSum.c
#include<stdio.h>
int main()
{
int a=10,b=15;
Sum(a,&b);
printf("%d\n",b);
}
// ASM Code
// Sum.asm
global _Sum
SEGMENT .text
[BITS 32]
_Sum:
push ebp
mov ebp,esp
mov eax,[ebp+8]
mov ebx,[ebp+12] ;The BYTE PTR didn't work
add eax,[ebx] ;some one told me PTR is not supported in NASM
mov [ebx],eax ;Is that true ?
pop ebp
ret