Emuforums.com

Go Back   Emuforums.com > General Discussion > Web development / Programming
About Us Register FAQ Members List Calendar Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old April 3rd, 2004   #1 (permalink)
Knowledge is the solution
 
Proto's Avatar
 
Join Date: Dec 2002
Location: Mexico City, Mexico
Posts: 5,565
Assembly help...

Ok as this month task i have to program the Hanoi towers in assembly... my only problem is that i pretty noobish in using extern function in asm files, so id ask for any advice you can give

My C++ calling program

#include [iostream.h]
#include [stdio.h]
#include [stdlib.h]

extern "C" int n;
extern "C" void principal();

int main(void){


cout <<"Numero de discos\n ";
cin >> n;

principal();


return 0;

}


The asm module

extern printf
global _n
global _principal

section .data
ind dd 0 ;;i use the indicator as a medium to know when to use the base ;; ;; case
_n dd 0
temp dd 0
fmt: db "se mueve %d del palo %d al palo %d", 10, 0

section .text

_principal

mov ax,1
mov bx,2
mov cx,3
mov dx,[_n]


Hanoi:
mov ax,1
cmp [ind],ax
JNZ general
push dx ;;base case, we move one disk from one stick to another
push ax
push cx
call printf ;;im asumming that the printf function gets the pops anyway ;; from what i've read

ret


ret ;;when we are done we return the control to C++

Code continues later about the general case

the error i am getting is


Linking...
proyecto.obj : error LNK2001: unresolved external symbol printf
Debug/Ensamblador.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Ensamblador.exe - 2 error(s), 0 warning(s)

Any clues?

EDIT: aaa...nm i realized the mistake. it was _printf
__________________



Last edited by Proto; April 3rd, 2004 at 17:07.
Proto is online now   Reply With Quote
Old April 3rd, 2004   #2 (permalink)
Knowledge is the solution
 
Proto's Avatar
 
Join Date: Dec 2002
Location: Mexico City, Mexico
Posts: 5,565
Sorry for double posting but i have another problem. The printf function called from the asm doesnt... print anything Can you point me out any mistakes you see please

extern _printf
global _n
global _principal

section .data
ind dd 0 ;;con este sabremos si solo se va a mover un disco
_n dd 0 ;;numero de discos
temp dd 0
text: db "se mueve %d del palo %d al palo %d", 10, 0

section .text

_principal

mov ax,1
mov bx,2
mov cx,3
mov dx,[_n]
mov [ind],dx



Hanoi:
mov [temp],bx
mov bx,1
cmp [ind],bx
mov bx,[temp]
JNZ general
push dx
push ax
push cx
call _printf
pop bx
pop bx
pop bx
ret

ret ;;regresa al modulo de c++

general:
---------
__________________



Last edited by Proto; April 3rd, 2004 at 19:49.
Proto is online now   Reply With Quote
Old April 8th, 2004   #3 (permalink)
Da Ultimate Noob Molester
 
_hyde_'s Avatar
 
Join Date: Sep 2003
Location: Los Angeles - CA
Posts: 125
Well, could be either one of the following things:

- You are not pushing the parameters used by printf in the right order (last argument ought to be pushed first).

- Also, I believe you are trying to print the string found inside text, but you haven't actually pushed a pointer to that string. Try something like this:

mov si, text ;stores the address of text in si
...
push si ;passes the parameter to the printf function.

Before you do any of this, however, remember to save the contents of things like bp, sp, si, and di (hell, you might as well just do a pusha right at the beginning of the function)

Hope this helps.

PS.: if you have any questions regarding assembly programming visit http://assembly.conforums.com/

EDIT: Like I thought, you only pushed *three* parameters instead of four!
__________________
_hyde_ is offline   Reply With Quote
Old April 12th, 2004   #4 (permalink)
Knowledge is the solution
 
Proto's Avatar
 
Join Date: Dec 2002
Location: Mexico City, Mexico
Posts: 5,565
....whoops

Yeah and i recover my registers later with a popa, the function that calls Hanoi.... thanks for your help
__________________


Proto is online now   Reply With Quote
Old April 12th, 2004   #5 (permalink)
Emu author
 
Join Date: Apr 2001
Location: Bloomington IN, USA
Posts: 1,061
I'm wondering why you're using the 16bit registers with 32bit data.. it'll work, if you don't overflow the 16bit space, but it still doesn't seem to make a lot of sense.

- Exo
Exophase is offline   Reply With Quote
Old April 12th, 2004   #6 (permalink)
Knowledge is the solution
 
Proto's Avatar
 
Join Date: Dec 2002
Location: Mexico City, Mexico
Posts: 5,565
Hmmm....i dont remember well...i guess it was because i was thinking that the pusha and popa only worked with the 16 bit part.... anyway im done with my code (it works now),here it is for those who need it (yeah sure )

Code:
extern	_printf
global _n
global _principal



section .data
ind dd 0  ;con este sabremos si solo se va a mover un disco
_n dd 0   ;numero de discos
temp dd 0

text db 'se mueve %d del palo %d al palo %d', 10, 0

section .text

_principal

mov ax,1  ;;se usan los registros para representar los palos
mov bx,2
mov cx,3 
mov dx,[_n]  ;;en dx se guarda el numero de discos
mov [ind],dx 



Hanoi:
mov [temp],bx
mov bx,1
cmp [ind],bx   ;;si ind vale uno es que solo se mueve un disco
mov bx,[temp]
JNZ general
pusha
push	ebp
mov	ebp,esp
mov [temp],cx
push  dword [temp]  ;;caso base Se lee: Se mueve el disco edx de eax a ecx

mov [temp],ax
push  dword [temp]

mov [temp],dx
push  dword [temp]

push dword text
call _printf
mov	esp,ebp
pop	ebp
popa
ret

ret ;;regresa al modulo de c++

general:
pusha            ;; movemos n-1 al palo auxiliar  
mov [temp], bx   
mov bx, cx
mov cx, [temp]
dec dx
mov [ind],dx
call Hanoi


popa             ;; uno al palo final
pusha
mov [temp],bx
mov bx,1
mov [ind],bx
mov bx,[temp]
call Hanoi


          ;;n-1 al palo central

popa
mov [temp], ax
mov ax, bx
mov bx, [temp]
dec dx
mov [ind], dx

call Hanoi
ret
__________________


Proto is online now   Reply With Quote
Old April 14th, 2004   #7 (permalink)
Da Ultimate Noob Molester
 
_hyde_'s Avatar
 
Join Date: Sep 2003
Location: Los Angeles - CA
Posts: 125
Yea, I suppose you shouldn't really be using 16-bit regs to store 32-bit values. So, if you are just going to be entering 16-bit numbers to test your program, declare your vars as dw.
__________________
_hyde_ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 17:24.

© 2006 - 2008 Emu Forums | About Emu Forums | Legal | A member of the Crowdgather Forum Community


Powered by vBulletin® Version 3.7.0 Release Candidate 3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5