about summary refs log tree commit diff
path: root/amd64.e
diff options
context:
space:
mode:
Diffstat (limited to 'amd64.e')
-rw-r--r--amd64.e49
1 files changed, 45 insertions, 4 deletions
diff --git a/amd64.e b/amd64.e
index ba78bc4..1a5df04 100644
--- a/amd64.e
+++ b/amd64.e
@@ -518,10 +518,24 @@ s" :cc-greater" keyword
     0 4 :rsp reg64 sib
     ~ : drop-subitem
   } if
+
   ~ The displacement byte.
+  ~
+  ~   When addressing-disp8-reg64 references a source, as in
+  ~ lea-reg64-disp8-reg64, the first two parameters of the assembly word
+  ~ calling it are the source register and the source displacement value. When
+  ~ it references a target or destination, as in mov-disp8-reg64-reg64, the
+  ~ LAST two parameters are the target registera and target displacement
+  ~ value.
+  ~
+  ~   In both cases, the register comes first and the displacement value
+  ~ comes second. When these are used in a comment template without any other
+  ~ rearranging going on, whichever is on the top of the stack will be popped
+  ~ off first. So, we swap them here, thus putting the register on the top.
   swap
   ~ : 1 adjust-length
   ~ : provide-hex8
+  ~ : swap-subitems
   pack8 ;
 
 ~ (output point, reg/op field value, reg/mem field register,
@@ -538,10 +552,13 @@ s" :cc-greater" keyword
     0 4 :rsp reg64 sib
     ~ : drop-subitem
   } if
-  ~ The displacement value.
+
+  ~   The displacement value. This is subject to the same considerations as in
+  ~ addressing-disp8-reg64.
   swap
   ~ : 4 adjust-length
   ~ : provide-hex32
+  ~ : swap-subitems
   pack32 ;
 
 ~ (output point, reg/op field value,
@@ -552,7 +569,17 @@ s" :cc-greater" keyword
   dup :rbp != {
     ~ Reg/mem value 4 means to use an SIB byte (at least, with this mode).
     5 roll 0 6 roll 4 modrm 4 unroll
-    reg64 3unroll reg64 3unroll scalefield 3unroll sib
+    reg64 3unroll reg64 3unroll scalefield 3unroll
+    ~   We want the scale factor to show after the registers, so we put it
+    ~ last on the stack. We also want the base register to show before the
+    ~ index register, so the base comes first.
+    ~
+    ~   Importantly, we have to do this before the sib so it'll count as being
+    ~ inside the span the assembly word's description attaches to.
+    ~
+    ~ : -3 roll-subitems
+    ~ : swap-subitems
+    sib
     exit
   } if
   ." Base parameter to addressing-indexed-reg64 is :rbp." 1 sys-exit ;
@@ -568,7 +595,21 @@ s" :cc-greater" keyword
   swap
   ~ : 1 adjust-length
   ~ : provide-hex8
-  pack8 ;
+  ~
+  ~   We want the parameters to be shown in the order:
+  ~
+  ~ base register, index register, index scale factor, displacement
+  ~
+  ~   So, we put them on the stack in the opposite of that order.
+  ~
+  ~   Importantly, we have to do this before the pack8 so it'll count as being
+  ~ inside the span the assembly word's description attaches to.
+  ~
+  ~ : -4 roll-subitems
+  ~ : -3 roll-subitems
+  ~ : swap-subitems
+  pack8
+  ;
 
 
 ~ Easy instructions
@@ -687,7 +728,7 @@ s" :cc-greater" keyword
 
 ~ (output point, source register, target register -- output point)
 : mov-reg64-reg64
-  ~ : 1 #swap # # mov-reg64-reg64
+  ~ : 1 # # mov-reg64-reg64
   3roll rex-w 0x89 pack8 3unroll
   swap reg64 swap addressing-reg64 ;
 : mov-indirect-reg64-reg64