# learning each /: \\:

**URL:** https://forum.kx.com/t/learning-each/7534
**Category:** Community Support
**Tags:** kdb-and-q
**Created:** [January 14, 2012, 4:48am UTC](https://forum.kx.com/t/learning-each/7534 "2012-01-14T04:48:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)
#### Post date: [January 14, 2012, 4:48am UTC](https://forum.kx.com/t/learning-each/7534/1 "2012-01-14T04:48:00Z")

</div>

hi guys,

how can i shorten the second expression here??

i’m sure i’ve got too many enlist /: , : words.

q)r:(“eax”;“ecx”;“edx”)

q)raze raze (enlist each (“mov”;“add”)){x,/:y}/::r{enlist x,y}/::r ?

“mov” “eaxeax”

“mov” “eaxecx”

“mov” “eaxedx”

“mov” “ecxeax”

“mov” “ecxecx”

“mov” “ecxedx”

“mov” “edxeax”

“mov” “edxecx”

“mov” “edxedx”

“add” “eaxeax”

“add” “eaxecx”

“add” “eaxedx”

“add” “ecxeax”

“add” “ecxecx”

“add” “ecxedx”

“add” “edxeax”

“add” “edxecx”

“add” “edxedx”

ta, jack

---

<div class="post-metadata">

### Author: ![trieder](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@trieder](https://forum.kx.com/u/trieder)
#### Post date: [January 14, 2012, 5:03am UTC](https://forum.kx.com/t/learning-each/7534/2 "2012-01-14T05:03:00Z")

</div>

I’m sure there’s a better way, but this is all you get for a Friday night at midnight. How did you leave ebx out? I mean common he’s all about the array crowd ;-)

q)r

“eax”

“ecx”

“edx”

q)a

"mov "

"add "

q)a cross","sv’3 cut’r cross r

“mov eax,eax”

“mov eax,ecx”

“mov eax,edx”

“mov ecx,eax”

“mov ecx,ecx”

“mov ecx,edx”

“mov edx,eax”

“mov edx,ecx”

“mov edx,edx”

“add eax,eax”

“add eax,ecx”

“add eax,edx”

“add ecx,eax”

“add ecx,ecx”

“add ecx,edx”

“add edx,eax”

“add edx,ecx”

“add edx,edx”

---

<div class="post-metadata">

### Author: ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)
#### Post date: [January 15, 2012, 9:15am UTC](https://forum.kx.com/t/learning-each/7534/3 "2012-01-15T09:15:00Z")

</div>

i’m quite happy with this now:

q).q.X:cross;.q.XX:{x X x}  
q)(4 cut"mov add “)X”,“sv’3 cut’XX"e”,'2 cut"axcxdxbxbpsidi"  
“mov eax,eax”  
“mov eax,ecx”  
“mov eax,edx”  
“mov eax,ebx”  
“mov eax,ebp”  
..

but disappointed that i can’t write something like this (in k):

(0 4\_“mov add “)X”,”:/'0 3\_'XX"e",'0 2\_“axcxdxbxbpsidi”

---

<div class="post-metadata">

### Author: ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)
#### Post date: [January 17, 2012, 3:42am UTC](https://forum.kx.com/t/learning-each/7534/4 "2012-01-17T03:42:00Z")

</div>

To: [personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)  
X-Mailer: Apple Mail (2.1084)

\> but disappointed that i can’t write something like this (in k):  
\>  
\> (0 4\_“mov add “)X”,”:/'0 3\_'XX"e",'0 2\_“axcxdxbxbpsidi”

.q.X[.q.cut[4]“mov add “]@”,”/:‘3 .q.cut’.q.XX"e",'.q.cut[2]“axcxdxbxbpsidi”

or

.q.X[0N 4#“mov add “]@”,”/:'0N 3#/:.q.XX"e",'0N 2#“axcxdxbxbpsidi”

incidentally to me the most natural solution to the original question is

q)r:enlist each(“eax”;“ecx”;“edx”)  
q)o:enlist each(“mov”;“add”)  
q)o cross r cross r  
“mov” “eax” “eax”  
“mov” “eax” “ecx”  
“mov” “eax” “edx”  
“mov” “ecx” “eax”  
“mov” “ecx” “ecx”  
“mov” “ecx” “edx”  
“mov” “edx” “eax”  
“mov” “edx” “ecx”  
“mov” “edx” “edx”  
“add” “eax” “eax”  
“add” “eax” “ecx”  
“add” “eax” “edx”  
“add” “ecx” “eax”  
“add” “ecx” “ecx”  
“add” “ecx” “edx”  
“add” “edx” “eax”  
“add” “edx” “ecx”  
“add” “edx” “edx”

or

q)raze o,/::raze r,/::r  
“mov” “eax” “eax”  
“mov” “eax” “ecx”  
“mov” “eax” “edx”  
“mov” “ecx” “eax”  
“mov” “ecx” “ecx”  
“mov” “ecx” “edx”  
“mov” “edx” “eax”  
“mov” “edx” “ecx”  
“mov” “edx” “edx”  
“add” “eax” “eax”  
“add” “eax” “ecx”  
“add” “eax” “edx”  
“add” “ecx” “eax”  
“add” “ecx” “ecx”  
“add” “ecx” “edx”  
“add” “edx” “eax”  
“add” “edx” “ecx”  
“add” “edx” “edx”

---

<div class="post-metadata">

### Author: ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)
#### Post date: [January 17, 2012, 4:31am UTC](https://forum.kx.com/t/learning-each/7534/5 "2012-01-17T04:31:00Z")

</div>

hi aaron,below…On Tue, Jan 17, 2012 at 2:42 PM, Aaron Davies \<aaron.davies\> wrote:\>\> but disappointed that i can’t write something like this (in k):\>\>\>\> (0 4\_“mov add “)X”,”:/'0 3\_'XX"e",‘0 2\_“axcxdxbxbpsidi”\>\> .q.X[.q.cut[4]“mov add “]@”,”/:‘3 .q.cut’.q.XX"e",’.q.cut[2]"axcxdxbxbpsidi"i was disappointed that i can’t define X as an infix operator in k(ie. use X without .q.X qualification)\> incidentally to me the most natural solution to the original question is\>\> q)r:enlist each(“eax”;“ecx”;“edx”)\> q)o:enlist each(“mov”;“add”)\> q)o cross r cross r\> “mov” “eax” “eax”\> “mov” “eax” "ecx"yes, this is the essence of the expression. i have the cuts on inputdata so i can show my input and intention in one line - maybe 3 lineswould have been better. what would arthur do?ta, jack.\</aaron.davies\>

---

<div class="post-metadata">

### Author: ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)
#### Post date: [January 19, 2012, 6:55am UTC](https://forum.kx.com/t/learning-each/7534/6 "2012-01-19T06:55:00Z")

</div>

To: [personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)  
X-Mailer: Apple Mail (2.1084)

\>\>\> but disappointed that i can’t write something like this (in k):  
\>\>\>  
\>\>\> (0 4\_“mov add “)X”,”:/'0 3\_'XX"e",‘0 2\_“axcxdxbxbpsidi”  
\>\>  
\>\> .q.X[.q.cut[4]“mov add “]@”,”/:‘3 =  
.q.cut’.q.XX"e",’.q.cut[2]“axcxdxbxbpsidi”  
\>  
\> i was disappointed that i can’t define X as an infix operator in k  
\> (ie. use X without .q.X qualification)

yes, unfortunately k has neither of the blessed namespace behaviors =  
(resolving and infix)

\>\> incidentally to me the most natural solution to the original question =  
is  
\>\>  
\>\> q)r:enlist each(“eax”;“ecx”;“edx”)  
\>\> q)o:enlist each(“mov”;“add”)  
\>\> q)o cross r cross r  
\>\> “mov” “eax” “eax”  
\>\> “mov” “eax” “ecx”  
\>  
\> yes, this is the essence of the expression. i have the cuts on input  
\> data so i can show my input and intention in one line - maybe 3 lines  
\> would have been better. what would arthur do?

i don’t know, i might do

q){x," “,y,”,“,z}.'{(cross)over(enlist each x;y;y:enlist each =  
y)}[(“mov”;“add”);“e”,'2 cut"axcxdxbxbpsidi”]

k){x," “,y,”,",z}.'{.q.cross/(,:'x;y;y:,:'y)}[(“mov”;“add”);“e”,'0N =  
2#“axcxdxbxbpsidi”]

if i wanted to keep it on one line

i might prefer something like

q){x," “,y,”,“,z}.'string{(cross)over(x;y;y)}[`mov`add;`$“e”,'2 =  
cut"axcxdxbxbpsidi”]

k){x," “,y,”,",z}.'${.q.cross/(x;y;y)}[`mov`add;`$“e”,'0N =  
2#“axcxdxbxbpsidi”]

though, since conceptually the opcodes and registers are enums =  
(independent of how you generate them)

(my best guess at arthur’s style might be something like

k){" “/:',/(,:‘0N 3#x),/::,:’”,"/:'y:,/y,/::y:,:'y}[“movadd”]“e”,'0N =  
2#“axcxdxbxbpsidi”

taking it as a string-manipulation problem, or

k){,/'${,/z,/::y,'x}/[y;", =  
";(y;x)]}[`mov`add]`$"e",'$`ax`cx`dx`bx`bp`si`di

as a list-manipulation problem)=

---

<div class="post-metadata">

### Author: ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)
#### Post date: [January 19, 2012, 9:37am UTC](https://forum.kx.com/t/learning-each/7534/7 "2012-01-19T09:37:00Z")

</div>

hi aaron,\> i might dothanks for your code - i am studying it, i see how useful projection is.\> though, since conceptually the opcodes and registers are enums (independent of how you generate them)actually, my aim is to see how an assembler deals with differentinput… i’ve got an i686 reference, but it’s good to confirm withassembler output. then a larger goal is to generate machine languagedirectly.(following the leader)ta, jack
