prime numbers
this is an exercies i set out to do in order to familiarise my self with linux and AT&T assembly syntax.
i worked a lot and read a lot before figureing out how each operator gets its paramaters and such...
the code doesn't compile with the following errors:
# as -gstabs sieve.s -o sieve.o # gcc sieve.o -o sieve sieve.o(.text+0x1):sieve.s:20: relocation truncated to fit: R_386_8 .data sieve.o(.text+0xc):sieve.s:24: relocation truncated to fit: R_386_16 .data sieve.o(.text+0x19):sieve.s:32: relocation truncated to fit: R_386_8 .data sieve.o(.text+0x1b):sieve.s:33: relocation truncated to fit: R_386_8 .data sieve.o(.text+0x2d):sieve.s:43: relocation truncated to fit: R_386_8 .data sieve.o(.text+0x39):sieve.s:50: relocation truncated to fit: R_386_8 .data collect2: ld returned 1 exit status
the source code is:
# assembly code for the calculation of the primes numbers in the range of [2-100] # the code brute forces the numbers out, no formulas are taken in consideration
*.include* "defines.h" #system call numbers are stored here (i.e. __NR_<oper> = number)
*.global* main
*.data*
sieve:
*.byte* 4 test: *.byte* 1
*.text*
main:
loop:
movb $test, %dl
incb %dl #increase test
movb %dl, (test)
movw $sieve, %ax #prepare for divb: move sieve in ax
divb %dl #div ax by dl
xor %cx, %cx #clear cx
movb %ah, %cl #move rest in cx
jcxz next #test if rest is 0 and increase if so
movb $sieve, %al
movb $test, %bl
cmpb %al, %bl #else test if "test" has reached sieve
jnz loop #if not go back and div by new test value
print: movl $__NR_write, %eax #else print the prime
movl $1, %ebx #STDOUT is $1 xor %ecx, %ecx movb $sieve, %cl addb $30, %cl #make ascii out of int movl $16,%edx #buffer size int $0x80
next:
movb $sieve, %cl
incb %cl #increase sieve
movb %cl, (sieve)
movb $100, %al
cmpb %al, %cl #test if limit is reached
jz exit
movb $1, %al #if not start again with a new sieve and test = 1
movb %al, (test)
jmp loop
exit: movl $0, %eax
ret
please send me any critique and advices you can, i really want to make the complete switch from MASM to GAS as soon as possible....
cheers!
Paul Irofti wrote:
...
Quoted 1 line
sieve.o(.text+0x1):sieve.s:20: relocation truncated to fit:
R_386_8.data
...
Quoted 1 line
movb $test, %dl
Pretty sure you want "movb test, %dl" - "$test" indicates that you're trying to move the *address* of your "test" variable (not the contents) into %dl... and it won't fit! (...etc.)
Best,
Frank
Quoted 14 lines · Frank Kotler
Paul Irofti wrote:
...
Quoted 1 line
sieve.o(.text+0x1):sieve.s:20: relocation truncated to fit:
R_386_8.data...
Quoted 1 line
movb $test, %dl
Pretty sure you want "movb test, %dl" - "$test" indicates that you're trying to move the *address* of your "test" variable (not the contents) into %dl... and it won't fit! (...etc.)
Quoted 8 lines
Best,
Frank-
To unsubscribe from this list: send the line "unsubscribe linux-assembly" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
ok, so than what's the diffrence between:
a) movb $test, %al
b) movb test, %al
c) movb (test), %al
c) movb ($test), %al #if this exists!
and how come when i access kernel ops i use $<oper>
i.e. movl $__NR_write, %eax #i actually append the address 1?! not put the value 1?!
these are very confusing stuff for me. and i would greatly apreciate it if explained! thanks a lot Frank!
Paul Irofti wrote:
...
Quoted 5 lines
ok, so than what's the diffrence between:
a) movb $test, %al
b) movb test, %al
c) movb (test), %al
c) movb ($test), %al #if this exists!
Hmmm, I'm actually Nasmist, not an as user, but...
Quoted 3 lines
and how come when i access kernel ops i use $<oper>
i.e. movl $__NR_write, %eax #i actually append the address 1?! not put the value 1?!
I perhaps shouldn't have said "address"... As I understand it, the '$' indicates an "immediate" value. "movl $4, %eax" (or $__NR_write) moves the value 4 into %eax. "movl 4, %eax" would move the contents of address 0x00000004 into %eax - except that it segfaults because 0x00000004 isn't in "your" address space.
Maybe an actual (G)as user can clarify it better. It *is* confusing - if it were easy, everybody'd be doing it! :)
Best,
Frank
Quoted 18 lines · Frank Kotler
Paul Irofti wrote:
...
Quoted 5 lines
ok, so than what's the diffrence between:
a) movb $test, %al
b) movb test, %al
c) movb (test), %al
c) movb ($test), %al #if this exists!Hmmm, I'm actually Nasmist, not an as user, but...
Quoted 3 lines
and how come when i access kernel ops i use $<oper>
i.e. movl $__NR_write, %eax #i actually append the address 1?! not put the value 1?!
Quoted 8 lines
I perhaps shouldn't have said "address"... As I understand it, the '$' indicates an "immediate" value. "movl $4, %eax" (or $__NR_write) moves the value 4 into %eax. "movl 4, %eax" would move the contents of address
0x00000004into %eax - except that it segfaults because0x00000004isn't in "your" address space.Maybe an actual (G)as user can clarify it better. It *is* confusing - if it were easy, everybody'd be doing it! :)
Best,
Frank
thanks man!
i had the same problems with syntax and address/value access in MASM when i started, now, i see myself hitting the same spot in the *nix world of assembly:))
well, i'm blown off, it's 6am here, i need some sleep, hopefully someone will make my morning..eh! afternoon... sweeter with a short lesson on the issue!
bye!