Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #60291 > unrolled thread

Gforth assembler examples for AMD64

Started bykrishna.myneni@ccreweb.org
First post2017-06-04 06:29 -0700
Last post2017-06-06 19:38 -0700
Articles 9 — 3 participants

Back to article view | Back to comp.lang.forth


Contents

  Gforth assembler examples for AMD64 krishna.myneni@ccreweb.org - 2017-06-04 06:29 -0700
    Re: Gforth assembler examples for AMD64 anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-06-04 15:25 +0000
      Re: Gforth assembler examples for AMD64 krishna.myneni@ccreweb.org - 2017-06-04 09:31 -0700
        Re: Gforth assembler examples for AMD64 anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2017-06-04 16:46 +0000
          Re: Gforth assembler examples for AMD64 krishna.myneni@ccreweb.org - 2017-06-04 16:13 -0700
            Re: Gforth assembler examples for AMD64 krishna.myneni@ccreweb.org - 2017-06-04 19:38 -0700
              Re: Gforth assembler examples for AMD64 Bernd Paysan <bernd@net2o.de> - 2017-06-07 01:00 +0200
                Re: Gforth assembler examples for AMD64 krishna.myneni@ccreweb.org - 2017-06-06 19:05 -0700
                Re: Gforth assembler examples for AMD64 krishna.myneni@ccreweb.org - 2017-06-06 19:38 -0700

#60291 — Gforth assembler examples for AMD64

Fromkrishna.myneni@ccreweb.org
Date2017-06-04 06:29 -0700
SubjectGforth assembler examples for AMD64
Message-ID<7173b951-3d0f-4dd4-b86e-4fd28fbcdde1@googlegroups.com>
Beyond the one example of assembly language programming in gforth, for AMD64, in the user's manual, are there some other abi-code examples one can use to learn this assembler?

Krishna
 

[toc] | [next] | [standalone]


#60293

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2017-06-04 15:25 +0000
Message-ID<2017Jun4.172529@mips.complang.tuwien.ac.at>
In reply to#60291
krishna.myneni@ccreweb.org writes:
>Beyond the one example of assembly language programming in gforth, for AMD64, in the user's manual, are there some other abi-code examples one can use to learn this assembler?

The assembler syntax is a variant of Bernd Paysan's 386 assembler
syntax, so look in the section on that for a general feeing.  A source
of examples for the assembler syntax is arch/amd64/testasm.fs.  For
ABI-CODE background, there is the paper

<http://www.complang.tuwien.ac.at/anton/euroforth/ef10/papers/ertl.pdf>

but it uses Intel syntax for the assembler (which does not work with
Gforth), because that is likely to be better known to the reader than
Bernd Paysan's syntax.

Finally, for using ABI-CODE, you may want to look at the ABI of your
platform.  For AMD64, this unfortunately varies between Windows and
the rest of the world.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2017: http://www.euroforth.org/ef17/

[toc] | [prev] | [next] | [standalone]


#60299

Fromkrishna.myneni@ccreweb.org
Date2017-06-04 09:31 -0700
Message-ID<286724d6-1297-468e-9b08-dc188c10a57f@googlegroups.com>
In reply to#60293
On Sunday, June 4, 2017 at 10:44:34 AM UTC-5, Anton Ertl wrote:
> krishna...org writes:
> >Beyond the one example of assembly language programming in gforth, for AMD64, in the user's manual, are there some other abi-code examples one can use to learn this assembler?
> 
> The assembler syntax is a variant of Bernd Paysan's 386 assembler
> syntax, so look in the section on that for a general feeing.  A source
> of examples for the assembler syntax is arch/amd64/testasm.fs. 
...

I used to be familiar with BP's 386 syntax. There is no testasm.fs in arch/amd64 , but there are in other arch subdirs:

$ find . -name testasm.fs
./mips/testasm.fs
./alpha/testasm.fs
./lm32/testasm.fs

On my system, the example in the Gforth manual works.

\ Example from Gforth manual

abi-code my+ ( n1 n2 -- n3 )
  8 di d) ax   lea
  di )    dx   mov
  dx      ax ) add
               ret
end-code


Next, I tried the most simple possible code word example, and, it too works fine.

\ most basic example
\ take stack ptr in di and move it
\ to ax where it is expected on return

abi-code basic ( -- )
 di  ax  mov
         ret
end-code


The next example also works,

\ next most basic example
\ return zero on the stack

abi-code bas2 ( -- 0 )
   di   ax   mov
   8 #  ax   sub
   0 #  dx   mov
   dx   ax ) mov
\ .q 0 #  ax ) mov
             ret
end-code

However, I don't understand why the commented instruction above gives an error saying that a register was expected.

Anyway, I think I have enough to work with for now.

Krishna





[toc] | [prev] | [next] | [standalone]


#60301

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2017-06-04 16:46 +0000
Message-ID<2017Jun4.184657@mips.complang.tuwien.ac.at>
In reply to#60299
krishna.myneni@ccreweb.org writes:
>I used to be familiar with BP's 386 syntax. There is no testasm.fs in arch/amd64

http://git.savannah.gnu.org/cgit/gforth.git/tree/arch/amd64/testasm.fs

>\ .q 0 #  ax ) mov
...
>However, I don't understand why the commented instruction above gives an error saying that a register was expected.

I have no idea, but testasm.fs contains a line

$1234567890abcdef # ax mov

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2017: http://www.euroforth.org/ef17/

[toc] | [prev] | [next] | [standalone]


#60309

Fromkrishna.myneni@ccreweb.org
Date2017-06-04 16:13 -0700
Message-ID<b3f574ee-409c-4827-b6e6-ca8155daf483@googlegroups.com>
In reply to#60301
On Sunday, June 4, 2017 at 11:50:45 AM UTC-5, Anton Ertl wrote:
> krishna...org writes:
> >I used to be familiar with BP's 386 syntax. There is no testasm.fs in arch/amd64
> 
> http://git.savannah.gnu.org/cgit/gforth.git/tree/arch/amd64/testasm.fs
> 

Thanks. These are all examples of using SSE instructions, which will be very useful.


> >\ .q 0 #  ax ) mov
> ...
> >However, I don't understand why the commented instruction above gives an error saying that a register was expected.
> 
> I have no idea, but testasm.fs contains a line
> 
> $1234567890abcdef # ax mov
> 

That's an example of moving an immediate value into a register. The instruction I was trying to use is an indirect move, e.g. the following line from vm.s in kForth, for the gas assembler (AT&T syntax):

	movl $0, (%ebx)   \ equivalent: 0 # bx ) mov

Krishna

[toc] | [prev] | [next] | [standalone]


#60316

Fromkrishna.myneni@ccreweb.org
Date2017-06-04 19:38 -0700
Message-ID<b7688729-b3be-4b46-90c0-92fd81b61e5f@googlegroups.com>
In reply to#60309
On Sunday, June 4, 2017 at 6:13:30 PM UTC-5, krishna...@ccreweb.org wrote:
...
> 
> 	movl $0, (%ebx)   \ equivalent: 0 # bx ) mov

The above type of instruction works, e.g.

variable v

\ Set v to 4
abi-code v=4 ( -- )
    di   ax   mov
         bx   push
    v #  bx   mov
    4 #  bx ) mov
         bx   pop
         ret
end-code

However, storing an immediate value indirectly into the address contained in the rax register doesn't appear to work in an abi-code word for some reason:

  0 # ax ) mov

I checked to make sure this wasn't an illegal instruction, e.g. the following code assembles by the GNU assembler:
--------------------------------
L_test:
	movq $0, (%rax)
	ret


$ as instr_x86_64.s -a 
GAS LISTING instr_x86_64.s 			page 1


   1              	
   2              	L_test:
   3 0000 48C70000 		movq $0, (%rax)
   3      000000
   4 0007 C3       		ret
   5              	

GAS LISTING instr_x86_64.s 			page 2


DEFINED SYMBOLS
      instr_x86_64.s:2      .text:0000000000000000 L_test

NO UNDEFINED SYMBOLS
-------------------------


Krishna

[toc] | [prev] | [next] | [standalone]


#60412

FromBernd Paysan <bernd@net2o.de>
Date2017-06-07 01:00 +0200
Message-ID<oh7bvh$mui$1@dont-email.me>
In reply to#60316
Am 05.06.2017 um 04:38 schrieb krishna.myneni@ccreweb.org:
> However, storing an immediate value indirectly into the address contained in the rax register doesn't appear to work in an abi-code word for some reason:
> 
>    0 # ax ) mov

Line 273 in arch/amd64/asm.fs should be

: mov ( r/m reg / reg r/m / reg -- ) imm# @
   IF    assign#  reg?
...

I've no idea what the extra code there should have done, but it's 
causing the problem and doesn't any good thing.

The current git version fixes your problem, and it also fixes a problem 
with the disassembler, who didn't understand the 64 bit immediate move.

[toc] | [prev] | [next] | [standalone]


#60416

Fromkrishna.myneni@ccreweb.org
Date2017-06-06 19:05 -0700
Message-ID<a3381e91-3365-4790-a686-da17ceb7bd2d@googlegroups.com>
In reply to#60412
On Tuesday, June 6, 2017 at 6:00:23 PM UTC-5, Bernd Paysan wrote:
> Am 05.06.2017 um 04:38 schrieb krishna...org:
> > However, storing an immediate value indirectly into the address contained in the rax register doesn't appear to work in an abi-code word for some reason:
> > 
> >    0 # ax ) mov
> 
> Line 273 in arch/amd64/asm.fs should be
> 
> : mov ( r/m reg / reg r/m / reg -- ) imm# @
>    IF    assign#  reg?
> ...
> 
> I've no idea what the extra code there should have done, but it's 
> causing the problem and doesn't any good thing.
> 
> The current git version fixes your problem, and it also fixes a problem 
> with the disassembler, who didn't understand the 64 bit immediate move.

Yes, the following example now works. Thanks.

Krishna

\ return zero on the stack

abi-code bas2 ( -- 0 )
   di   ax   mov
   8 #  ax   sub
   0 #  ax ) mov
             ret
end-code

[toc] | [prev] | [next] | [standalone]


#60417

Fromkrishna.myneni@ccreweb.org
Date2017-06-06 19:38 -0700
Message-ID<c303f3a5-1247-4fcb-8e66-7dfd02c0e117@googlegroups.com>
In reply to#60412
On Tuesday, June 6, 2017 at 6:00:23 PM UTC-5, Bernd Paysan wrote:
...

The following code appears to mis-assemble :

----------------
abi-code get-pi ( -- ) ( F: -- pi )
     di   ax   mov
     8 #  si   sub
               fldpi
     si )      fstp
               ret 
end-code

see get-pi 
ABI-Code get-pi  
7FB4846571A0:	mov	rax,rdi
7FB4846571A3:	sub	rsi,$08
7FB4846571A7:	fxch	SFLOAT PTR -$15[eax]
7FB4846571AA:	fstp	[rsi]
7FB4846571AD:	ret
end-code
 ok
hex  ok
7FB4846571A0 10 dump 
7FB4846571A0: 48 8B C7 48  83 EE 08 D9 - 48 EB 48 DB  3E C3 00 00 
---------------

Here's the Gnu assembler equivalent code and it's assembled opcodes:

   1 0000 4889F8   		movq %rdi, %rax
   2 0003 4883EE08 		sub  $8,  %rsi
   3 0007 D9EB     		fldpi
   4 0009 D91E     		fstp (%rsi)
   5 000b C3       		ret

----------------

The FLDPI instruction assembles to D9 only, instead of to D9 EB. 

The instruction 

SI ) FSTP

also appears to be doing something else.

KM

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web