Question

I would like to have a scalar multiplication of two vectors using NASM and C. With the convention that I only declare function in C and implementation is in assembly. I have aproblem with that program, especially in assembly file. I don't know what I am doing wrong. I have some syntax errors but I don't know why they are in 27, 28 and 37 line of my code. Those are errors displayed after first line of compilation nasm -felf32 pos.asm -o pos.o:

pos.asm:27: error: comma or end of line expected
pos.asm:28: error: expression syntax error 
pos.asm:37: error: expression syntax error 

assembly code:

; nasm -felf32 pos.asm -o pos.o
; gcc -m32 -o zadanie1c.o -c zadanie1.c 
; gcc -m32 zadanie1c.o zadanie1a.o -o zadanie1

 %define n qword [ebp+8]
 %define zero qword [ebp+12]
 %define vect1 qword [ebp+20]
 %define vect2 qword [ebp+20+n*8]
 %define result qword [ebp+20+n*16]

 segment .data
 MinusFour dw -4

 segment .text
 global scalar
 scalar:

 push ebp
 mov ebp, esp
 push ebx                 ; 

fld zero ; for safety initialization
mov ecx,n

myLoop:

   fld vect1+ecx*8 ; v1
   fld vect2+ecx*8 ; v2
   fmulp st1 ; v1*v2
   faddp st1 ; v1*v2 + 
   cmp 0,ecx
   je end
   dec ecx
   jmp myLoop

end:
 mov ebx, result
 fstp qword [ebx]

 mov eax, ebx
 pop ebx
 mov esp, ebp
 pop ebp

 ret
Was it helpful?

Solution

%defines in NASM work like #defines in C - complete text replacement. So

 %define vect1 qword [ebp+20]
 %define vect2 qword [ebp+20+n*8]
 ...

   fld vect1+ecx*8 ; v1
   fld vect2+ecx*8 ; v2

becomes

   fld qword [ebp+20]+ecx*8 ; v1
   fld qword [ebp+20+n*8]+ecx*8 ; v2

which is the invalid syntax the compiler is complaining about.

How did I find this, even though I've never used NASM? I read the error and the line number. "Comma or end of line expected" sure sounds like a syntax error. Then I looked at everything in the line it was complaining about. What could be messing up the syntax? Hmm, vect2 isn't an assembly keyword, what is it? A %define... how do they work? Google says... " The definitions work in a similar way to C". Eureka!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top