Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #21759
| Newsgroups | comp.lang.forth |
|---|---|
| Date | 2013-04-19 18:21 -0700 |
| Message-ID | <eb60b3e1-b203-4589-a1be-e80a161005ea@googlegroups.com> (permalink) |
| Subject | Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... |
| From | AKE <assadebrahim2000@gmail.com> |
This is a question around getting better technique (in Forth) but also about working with the Forth assembler.
I've been playing around with W32 Forth's built-in x86 assembler that uses code ... next, end-code.
Win32F uses ebx as the TOS register, keeps eax and ecx as unrestricted registers. A little poking of the other registers suggest that edx, edi, esi registers are restricted, i.e. they must be restored to their original values before the code ... end-code block returns to Forth
The restricted registers are fine since most opcodes do not clobber all four general purpose registers... except the cpuid opcode (in 486 and above) does.
[ The cpuid instruction with eax=0 puts a byte value in eax, and *three* packed strings into ebx, ecx, edx respectively (4 bytes in each string). Unpacking the three strings (in the correct order) is supposed to give either "GenuineIntel" or "AuthenticAMD" according to chipset. ]
Usually, since there would be an un-clobbered and un-restricted register free, I could just move other values about a bit like a shell game.
But with 'cpuid' clobbering all 4 GP registers, *and* with EDX, ESI and EDI being *restricted* registers that must be restored before returning to Forth, it seems one has to come up with some other way.
I've got working code using the forward portion of the memory stack as a temporary register, and this works. But I am interested in whether there are better / perhaps more conventional ways to do this?
Any insights / tips would be appreciated.
Thanks in advance,
--------------------
My code is below for reference.
( Exploring Forth / Assembly interface. )
( Win32Forth on Windows machine )
CODE call-cpu-info
push ebx \ preserve TOS
( Protect edx by saving to unused part of stack memory )
mov ebx, esp
sub ebx, # 20 \ advance 5 cells ahead of current stack
mov [ebx], edx \ save protected register edx
( Run cpuid opcode )
xor eax, eax \ eax=0
cpuid \ returns eax=N (highest recognised param)
\ and three packed 4-byte chars to ebx,ecx,edx
( Send returned values to Forth stack )
push ecx \ 'ntel'
push edx \ 'ineI'
push ebx \ 'Genu'
push eax \ highest recognised param
( Restore original value to edx from unused part of stack... )
mov ebx, esp
sub ebx, # 4 \ ... which has now grown by 4 cells
mov edx, [ebx] \ restore edx
pop ebx \ leave N on TOS
next,
end-code
: LSB ( num -- byte ) \ 'bytes' off least significant byte (LSB)
$FF and \ mask off lowest byte
;
: chop-LSB ( num -- num/32.9 ) \ cuts off least significant byte
8 rshift ;
: emit-bytes-LE ( num -- ) \ emits bytes as characters
\ little-endian order, i.e. smallest byte first
cell 0
do
dup LSB emit
chop-LSB
loop drop ;
: cpu-info ( -- )
call-cpu-info ( d c b a ) drop
3 0 do
emit-bytes-LE
loop ;
Result (on my 32-bit WinXP machine):
Call:
cpu-info ok.
Result:
GenuineIntel
Back to comp.lang.forth | Previous | Next — Next in thread | Find similar
Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... AKE <assadebrahim2000@gmail.com> - 2013-04-19 18:21 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... "Rod Pemberton" <do_not_have@notemailnotq.cpm> - 2013-04-21 01:27 -0400
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... AKE <assadebrahim2000@gmail.com> - 2013-04-21 04:57 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... "Rod Pemberton" <do_not_have@notemailnotq.cpm> - 2013-04-21 11:39 -0400
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Alex McDonald <blog@rivadpm.com> - 2013-04-21 09:53 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... AKE <assadebrahim2000@gmail.com> - 2013-04-21 14:47 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Alex McDonald <blog@rivadpm.com> - 2013-04-22 02:17 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Coos Haak <chforth@hccnet.nl> - 2013-04-21 17:34 +0200
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Coos Haak <chforth@hccnet.nl> - 2013-04-21 17:42 +0200
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-04-21 12:37 -0500
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Alex McDonald <blog@rivadpm.com> - 2013-04-21 10:53 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-04-22 03:24 -0500
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Alex McDonald <blog@rivadpm.com> - 2013-04-22 02:49 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-04-22 10:06 +0000
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-04-22 07:35 -0500
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-04-22 13:12 +0000
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-04-22 10:01 -0500
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Alex McDonald <blog@rivadpm.com> - 2013-04-22 08:33 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-04-22 11:34 -0500
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Alex McDonald <blog@rivadpm.com> - 2013-04-23 03:27 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... "Rod Pemberton" <do_not_have@notemailnotq.cpm> - 2013-04-23 17:42 -0400
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-04-24 03:00 -0500
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... AKE <assadebrahim2000@gmail.com> - 2013-04-21 14:43 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Alex McDonald <blog@rivadpm.com> - 2013-04-22 02:21 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Alex McDonald <blog@rivadpm.com> - 2013-04-22 08:36 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... Alex McDonald <blog@rivadpm.com> - 2013-04-22 08:39 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... AKE <assadebrahim2000@gmail.com> - 2013-04-22 13:55 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... AKE <assadebrahim2000@gmail.com> - 2013-04-21 14:39 -0700
Re: Stack Interface between x86 Assembler & Forth -- technique when running out of unprotected registers... albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-04-21 21:00 +0000
csiph-web