Δημοσιεύτηκε: 13 Ιαν 2013, 21:56
από alkismavridis
****επεξεργασία****** αν μπείτε στην διαδικασία να δείτε τι κάνουν οι lib1.asm και lib.asm, διαβάστε πρώτα την lib2 :-)


Οκ δίνω τα εξής:
Στο αρχείο lib1.asm (αυτή την κατάληξη συνηθίζω, αλλά δε νομίζω ότι κάνει διαφορά...). Ας πούμε αυτή να είναι η στατική:

Κώδικας: Επιλογή όλων
global printNumber32R

[section .bss]
num resb 10



[section .text]




printNumber32R:   
   ;does the same with printNumber32, but doesn't affect any Register
   ;the number to print MUST be in eax
   ;ebx -> index of the memory to save the digit
   ;ecd -> Numbering system, wich is...10
   
   
   
   push rax ;save registers
   push rbx
   push rcx
   push rdx
   
   
   mov [num+10], byte 10 ;new line
   mov ebx, num
   add ebx, 9 ;aim to the last
   mov ecx,10 ;the divisor/numbering system. If you change this, you will get other numbering systems
   
   
   
   digitloop: ;-----------------------calculate the digits-----------------------
   mov edx,0 ;clean the remainder area!
   div ecx ;now the eax=result, edx=remainder
   
   add dl,30h ;convert to ascii and put it to the memory!
   mov [ebx],dl
   
   dec ebx ;go to the next digit's place, otherwise.
   cmp eax,0 ;finish if you wrote all digits
   je clearloop
   jmp digitloop
   
   
   clearloop: ;------------------------clear the higher digits from what they may had before---------------------
   cmp ebx,num
   jle clearend
   mov [ebx], byte 0
   dec ebx
   jmp clearloop
   clearend:
   

   mov eax, 4 ;----------------------write it!----------------------------
   mov ebx,1
   mov ecx, num
   mov edx, 11
   int 80h
   
   pop rdx ;restore registers
   pop rcx
   pop rbx
   pop rax
   ret


Στο αρχείο lib2.asm (η δυναμική)
Κώδικας: Επιλογή όλων
global printNumber32

[section .bss]
num resb 10

[section .text]



printNumber32:
   ;the number to print MUST be in eax
   
   ;ebx -> index of the memory to save the digit
   ;ecd -> Numbering system, wich is...10
   
   mov [num+10], byte 10 ;new line
   mov ebx, num
   add ebx, 9 ;aim to the last
   mov ecx,10 ;the divisor/numbering system. If you change this, you will get other numbering systems
   
   
   
   digitloop: ;-----------------------calculate the digits-----------------------
   mov edx,0 ;clean the remainder area!
   div ecx ;now the eax=result, edx=remainder
   
   add dl,30h ;convert to ascii and put it to the memory!
   mov [ebx],dl
   
   dec ebx ;go to the next digit's place, otherwise.
   cmp eax,0 ;finish if you wrote all digits
   je clearloop
   jmp digitloop
   
   
   clearloop: ;------------------------clear the higher digits from what they may had before---------------------
   cmp ebx,num
   jle clearend
   mov [ebx], byte 0
   dec ebx
   jmp clearloop
   clearend:
   

   mov eax, 4 ;----------------------write it!----------------------------
   mov ebx,1
   mov ecx, num
   mov edx, 11
   int 80h

   ret

(στην ουσία κάνει την ίδια δουλειά με την lib1.asm, αλλά δεν μπαίνει στον κόπο να σώσει τα registers)

και η main.asm:
Κώδικας: Επιλογή όλων
extern printNumber32, printNumber32R
global _start

[section .text]


_start:

   mov eax,159872
   call printNumber32R
   
   mov eax,2
   call printNumber32
   
   mov eax,1
   mov ebx,0
   int 80h


Αν πατήσεις τις εντολές
Κώδικας: Επιλογή όλων
alkis@Alkis:~/Programs/assembly/linking$ nasm -felf64 lib1.asm
alkis@Alkis:~/Programs/assembly/linking$ nasm -felf64 lib2.asm
alkis@Alkis:~/Programs/assembly/linking$ nasm -felf64 main.asm
alkis@Alkis:~/Programs/assembly/linking$ ld -o Program lib1.o lib2.o main.o
alkis@Alkis:~/Programs/assembly/linking$ ./Program
159872
2
alkis@Alkis:~/Programs/assembly/linking$

παιζει κανονικά.
Αλλά εδώ είναι όλα στατικά.
Αν πχ. πάω και σβήσω το lib2.o που έχει δημιουργηθεί, το πράγμα δουλεύει ακόμα...