Author Topic: linking to libraries?  (Read 7140 times)

AzAr

  • Guest
linking to libraries?
« on: November 09, 2008, 05:53:09 PM »
Hello,
A friend and I have been working with nasm, and have a few questions.
first, if I do something like:
int test();
int test()
{
return 32;
}
and set that as extern in my asm file, even when I call test, it says it's unknown: using the following syntax:
gcc -c test.c
nasm -f elf asmtest.asm
gcc asmtest.o test.o -o test
second, I'm trying to get rand working--any examples would be great.
Last, (I think), is there a way to print an int? I assume the int has to be converted to a string--not quite sure how to do that. After it's converted, I could just print it. I'd like to write a small guess the number program in order to learn more about how asm works, etc.
TIA,

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: linking to libraries?
« Reply #1 on: November 09, 2008, 08:43:04 PM »
Hi... Tyler or Tyler's friend(???),

I'm working on a reply from two or three messages ago on the nasm-users list... (going slowly 'cause I don't "like" C much)... If it comes out any good, I'll post it here, too...

You seem to have the general idea... Tyler just posted some code to nasm-users with the code in "section .data" - That ain't gonna work!

Despite the obviousness, "test" is a lousy name for your test - since it's both an instruction and a Unix utility. I've had problems with that!

extern int ttest();

int ttest()
{
return 32;
}

and...

global _start

extern ttest

section .text
_start:
call ttest
mov ebx, eax
mov eax, 1 ; __NR_exit
int 80h

("echo $?" shows "32")

If you want to link with gcc, "_start" is in crt0.o (or so) - use "main" instead, or the "--nostartlib" (or so) switch will do it(?). Or... don't use gcc, link with ld. The "gotcha" here is that ld, by default, uses the wrong dynamic linker, and has to be told "-I/lib/ld-linux.so2" (it's looking for .so1, and gets "file not found"!!!). Works for me...

Yeah, you need to convert number to ascii... ascii to number first, probably - user input is in ascii, not numbers(!).

Here's a "clever" atoi Herbert Kleebauer showed me - stolen from C compiler output(!)...

atoi:
    mov edx, [esp + 4]  ; pointer to string
    xor eax, eax        ; clear "result"
.top:
    movzx ecx, byte [edx]
    inc edx
    cmp ecx, byte '0'
    jb .done
    cmp ecx, byte '9'
    ja .done

; we have a valid character - multiply
    ; result-so-far by 10, subtract '0'
    ; from the character to convert it to
    ; a number, and add it to result.

lea eax, [eax + eax * 4]
    lea eax, [eax * 2 + ecx - 48]

jmp short .top
.done
    ret

I haven't got a "good" itoa on hand. This prints the number, but we want something that will convert it into a buffer - preferably with "field width", and optional right justification and all. This was intended more for "debugging" purposes than for a "nice display"...

;---------------------------------
showeaxd:
    push eax
    push ebx
    push ecx
    push edx
    push esi

sub esp, 10h
    lea ecx, [esp + 12]
    mov ebx, 10
    xor esi, esi
    mov byte [ecx], 0
.top:
    dec ecx
    xor edx, edx
    div ebx
    add dl, '0'
    mov [ecx], dl
    inc esi
    or eax, eax
    jnz .top

mov edx, esi
    mov ebx, 1
    mov eax, 4
    int 80h



add esp, 10h

pop esi
    pop edx
    pop ecx
    pop ebx
    pop eax

ret
;---------------------------------

Might as well throw in a hex version, too...

;------------------------------
showeaxh:
    push eax
    push ebx
    push ecx
    push edx

sub esp, 10h

mov ecx, esp
    xor edx, edx
    mov ebx, eax
.top:
    rol ebx, 4
    mov al, bl
    and al, 0Fh
    cmp al, 0Ah
    sbb al, 69h
    das
    mov [ecx + edx], al
    inc edx
    cmp edx, 8
    jnz .top
    mov ebx, 1
    mov eax, 4
    int 80h

add esp, 10h


    pop edx
    pop ecx
    pop ebx
    pop eax
    ret
;------------------------------    

Not very "virtuous" examples, but they work for me... Since we're soiling our pure asm with C anyway... this is where printf()/scanf() come in! Converting ints is easy, but ftoa/atof are a *royal* PITA!!!

I think Jeff Duntemann's "Step by Step" has a "rand" example. You're on the right track, but I think you wanna call srand() just once, not every time. Jeffs example is in linuxcode.zip from:

http://www.copperwood.com/pub/

( http://www.duntemann.com for his main page... anyone else remember "Carl and Jerry"? Great blast from the past!)

I'll get back to ya on the nasm-users list in more detail... sometime soon now...

Best,
Frank