return a value in asm functions

Programming, for all ages and all languages.
Post Reply
SkaCahToa
Posts: 1
Joined: Sat Mar 13, 2010 1:38 am

return a value in asm functions

Post by SkaCahToa »

hey, I've just learned some basic asm and c. I've programmed a basic 16bit x86 os, but I'm trying to write a function in asm that'll return a byte.

I want to be able to call the function from c code like this:

Code: Select all

char a;
a = pullFromMemory(0xB000, 0x8000);
The code I have for the asm function right now is:

Code: Select all

	.global _pullFromMemory

;char pullFromMemory (int segment, int address)
_pullFromMemory:
	push bp
	mov bp,sp
	push ds
	mov ax,[bp+4]
	mov si,[bp+6]
	mov ds,ax
	mov cl,[si]
	mov al,cl
	mov al,#0	;we only want Ah returned
	pop ds
	pop bp
	ret
However the code currently isn't working. Any ideas what's going on.

Im under the impression that the data in ah is the return value of the function... or am I mistaken?
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: return a value in asm functions

Post by bewing »

Well, the easy way to do this would just be to use a far pointer in C.

But otherwise it depends on exactly which compiler you are using for the C part of the code. Usually, return values are in EAX -- that is, you should be putting the byte in AL, not AH.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: return a value in asm functions

Post by qw »

(Missed bug.)
Last edited by qw on Tue Apr 06, 2010 1:59 pm, edited 1 time in total.
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: return a value in asm functions

Post by Gigasoft »

The bug is that it zeroes AL, not AH. The return value should be in AL. Another bug is that SI is not preserved.

Further improvements would be, use the LDS instruction, and use ESP instead of BP (assuming that the high word of ESP is zero). Or better yet, get a compiler that has far pointers.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: return a value in asm functions

Post by qw »

Most 16-bit compilers I know support far pointers.
bitshifter
Member
Member
Posts: 50
Joined: Sun Sep 20, 2009 4:03 pm

Re: return a value in asm functions

Post by bitshifter »

Also, where/when are the parameters removed from stack?
For stdcall you should retnw 4 assuming 16bit code...
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: return a value in asm functions

Post by Owen »

bitshifter wrote:Also, where/when are the parameters removed from stack?
For stdcall you should retnw 4 assuming 16bit code...
Presumably its a cdecl function...
Post Reply