0

im a newbie in Assembly language. I came across an Assembly code that computes a quadratic equation and is run on Ubuntu 64 bit.I'm trying to use printf to print out the integers but it's not working. The code is ;

global _main
section .text



extern printf,scanf

print:
mov eax,4
mov ebx,1
int 0x80
ret

main:

mov ecx,inputa
mov edx,linputa
call print

push a
push scan
call scanf

mov ecx,inputb
mov edx,linputb
call print

push b
push scan
call scanf

mov ecx,inputc
mov edx,linputc
call print

push c
push scan
call scanf

fld qword[b]
fmul st0
fld qword[a]
fmul qword[c]
mov word[const],4
fimul word[const]
fchs
fadd st1
fst qword[disc]

push dword[disc+4]
push dword[disc]
push dis
call printf

ftst
fstsw ax
sahf
ja real_roots
sahf
je one_root

imag_roots:
fchs
fsqrt
fld qword[b]
fchs
fadd st1
fld qword[a]
mov word[const],2
fimul word[const]
fxch st1
fdiv st1
fstp qword[x1]
fld qword[disc]
fchs
fsqrt
fld qword[b]
fadd st1
fchs
fld qword[a]
mov word[const],2
fimul word[const]
fxch st1
fdiv st1
fstp qword[x2]

push dword[x2+4]
push dword[x2]
push dword[x1+4]
push dword[x1]
push imagroot
call printf
jmp over

real_roots:
fsqrt
fld qword[b]
fchs
fadd st1
fld qword[a]
mov word[const],2
fimul word[const]
fxch st1
fdiv st1
fstp qword[x1]
fld qword[disc]
fsqrt
fld qword[b]
fadd st1
fchs
fld qword[a]
mov word[const],2
fimul word[const]
fxch st1
fdiv st1
fstp qword[x2]

push dword[x2+4]
push dword[x2]
push dword[x1+4]
push dword[x1]
push realroot
call printf

jmp over

one_root:
fsqrt
fld qword[b]
fchs
fadd st1
fld qword[a]
mov word[const],2
fimul word[const]
fxch st1
fdiv st1
fstp qword[x1]


push dword[x1+4]
push dword[x1]
push oneroot
call printf


over:
mov eax,1
mov ebx,0
int 0x80

section .bss
x1 resq 1
x2 resq 1
const resw 1
a resq 1
b resq 1
c resq 1
disc resq 1

section .data
scan dd "%lf",0
oneroot dd "Root = %f",10,0
realroot dd "Root 1 = %f    &   Root 2 = %f ",10,0
imagroot dd "Root 1 = %fi   &   Root 2 = %fi ",10,0


inputa dd "INPUT THE CO-EFFICIENT a ( a(x^2) + bx + c ) ",10
linputa equ $-inputa
inputb dd "INPUT THE CO-EFFICIENT b ( a(x^2) + bx + c ) ",10
linputb equ $-inputb
inputc dd "INPUT THE CO-EFFICIENT c ( a(x^2) + bx + c ) ",10
linputc equ $-inputc
dis dd "DISCRIMINANT  =  %f ",10,0

I assembled it using nasm -f elf qua.asm and tried linking it with ld -m elf_i386 -s -o qua qua.o

It gives me the following error

ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080 qua.o: In function main': qua.asm:(.text+0x27): undefined reference toscanf' qua.asm:(.text+0x45): undefined reference to scanf' qua.asm:(.text+0x63): undefined reference to scanf' qua.asm:(.text+0xa6): undefined reference to printf' qua.o: In functionimag_roots': qua.asm:(.text+0x13b): undefined reference to printf' qua.o: In functionreal_roots': qua.asm:(.text+0x1be): undefined reference to printf' qua.o: In functionone_root': qua.asm:(.text+0x201): undefined reference to `printf'

i tried linking it with gcc gcc -m32 -o qua qua.o

and i got an error

/usr/bin/ld: cannot find crt1.o: No such file or directory /usr/bin/ld: cannot find crti.o: No such file or directory /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/5/libgcc.a when searching for -lgcc /usr/bin/ld: cannot find -lgcc /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/5/libgcc_s.so when searching for -lgcc_s /usr/bin/ld: cannot find -lgcc_s /usr/bin/ld: cannot find -lc /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/5/libgcc.a when searching for -lgcc /usr/bin/ld: cannot find -lgcc /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/5/libgcc_s.so when searching for -lgcc_s /usr/bin/ld: cannot find -lgcc_s /usr/bin/ld: cannot find crtn.o: No such file or directory collect2: error: ld returned 1 exit status

How do you go about this?

Sala
  • 43
  • 10
  • 2
    You need to link the C startup and run-time libraries - the easiest way to do this is to use `gcc` instead of `ld`, e.g. `gcc -m32 -o qua qua.o`. – Paul R Feb 07 '18 at 08:54
  • @PaulR I did that too. I've edited the post again. – Sala Feb 07 '18 at 08:59
  • 2
    You probably don't have the 32 bit libraries installed - current Linux distros only have the 64 bit libraries by default on x86-64 installs. I can't remember the exact name of the package but you need something with "gcc" and "32" in the name. – Paul R Feb 07 '18 at 09:02
  • why not just build 64 bit? `gcc -o qua qua.o` – Uprooted Feb 07 '18 at 09:27
  • 1
    @user7231 because it's 32 bit assembly. – Jabberwocky Feb 07 '18 at 09:52
  • IMO better to change few `push dword` to `push qword` then to install 32 bit libraries for one example. If you plan to build for 32bit regularly then follow [this](https://stackoverflow.com/questions/22355436/how-to-compile-32-bit-apps-on-64-bit-ubuntu) – Uprooted Feb 07 '18 at 10:10
  • 1
    You'll likely need to install the multiarch versions of gcc/g++. If on Ubuntu based system try installing with `sudo apt-get install gcc-multilib g++-multilib` . On Debian you do it without `sudo`. – Michael Petch Feb 07 '18 at 13:00

0 Answers0