summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorCrystal <crystal@wizard.tower>2024-03-22 14:08:37 +0100
committerCrystal <crystal@wizard.tower>2024-03-22 14:08:37 +0100
commitedbb6a1c58b75a4be494a268d02240e3f4720b77 (patch)
tree9d19d0f32fffafc108052405cfb9e58ca518b221 /src
parente8c190c8b2984cd562ba65890ad86213a13b9e72 (diff)
downloadwww-edbb6a1c58b75a4be494a268d02240e3f4720b77.tar.gz
New
Diffstat (limited to 'src')
-rw-r--r--src/org/blog/assembly/1.org113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/org/blog/assembly/1.org b/src/org/blog/assembly/1.org
index e268824..3fd21e4 100644
--- a/src/org/blog/assembly/1.org
+++ b/src/org/blog/assembly/1.org
@@ -128,3 +128,116 @@ The values of the registers CS DS and SS are automatically initialized by the OS
 MOV DS, 0x0005 ; Is INVALID
 MOV DS, AX ; This one is VALID
 #+END_SRC
+
+* The ACTUAL thing :
+Enough technical rambling, and now we shall go to the fun part, the ACTUAL CODE. But first, some names you should be familiar with :
+
+- *Mnemonics* : Or *Instructions*, are the...well...Instructions executed by the CPU like *MOV* , *ADD*, *MUL*...etc, they are case *insensitive* but i like them better in UPPERCASE.
+- *Operands* : These are the options passed to the instructions, like *MOV dst, src*, and they can be anything from a memory location, to a variable to an immediate address.
+
+** Structure of an assembly program :
+While there is no "standard" structure, i prefer to go with this one :
+
+#+BEGIN_SRC asm
+    org 100h
+.data
+                                ; variables and constants
+
+.code
+                                ; instructions
+#+END_src
+** MOV dst, src
+The MOV instruction copies the Second operand (src) to the First operand (dst)... The source can be a memory location, an immediate value, a general-purpose register (AX BX CX DX). As for the Destination, it can be a general-purpose register or a memory location.
+
+
+these types of operands are supported:
+#+BEGIN_SRC asm
+MOV REG, memory
+MOV memory, REG
+MOV REG, REG
+MOV memory, immediate
+MOV REG, immediate
+#+END_SRC
+*REG*: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
+
+*memory*: [BX], [BX+SI+7], variable
+
+*immediate*: 5, -24, 3Fh, 10001101b
+
+
+for segment registers only these types of MOV are supported:
+#+BEGIN_SRC asm
+MOV SREG, memory
+MOV memory, SREG
+MOV REG, SREG
+MOV SREG, REG
+SREG: DS, ES, SS, and only as second operand: CS.
+#+END_SRC
+*REG*: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.
+
+*memory*: [BX], [BX+SI+7], variable
+
+*** Note : The MOV instruction *cannot* be used to set the value of the CS and IP registers
+** Variables :
+Let's say you want to use a specific value multiple times in your code, do you prefer to call it using something like *var1* or *E4F9:0011* ? If your answer is the second option, you can gladly skip this section, or even better, seek therapy.
+
+Anyways, we have two types of variables, *bytes* and *words(which are two bytes)*, and to define a variable, we use the following syntax
+
+#+BEGIN_SRC asm
+name DB value ; To Define a Byte
+name DW value ; To Define a Word
+#+END_SRC
+
+*name* - can be any letter or digit combination, though it should start with a letter. It's possible to declare unnamed variables by not specifying the name (this variable will have an address but no name).
+*value* - can be any numeric value in any supported numbering system (hexadecimal, binary, or decimal), or "?" symbol for variables that are not initialized.
+
+*** Example code :
+#+BEGIN_SRC asm
+    org 100h
+    .data
+    x db 33
+    y dw 1350h
+
+    .code
+    MOV AL, x
+    MOV BX, y
+#+END_SRC
+
+*** Arrays :
+We can also define Arrays instead of single values using comma separated vaues. like this for example
+#+BEGIN_SRC asm
+    a db 48h, 65h, 6Ch, 6Fh, 00H
+    b db 'Hello', 0
+#+END_SRC
+
+Surprise Surprise, the arrays a and b are identical, the reason behind it is that characters are first converted to their ASCII values then stored in memory!!! Wonderful right ? And guess what, accessing values in assembly IS THE SAME AS IN C !!!
+#+BEGIN_SRC asm
+    MOV AL, a[0] ; Copies 48h to AL
+    MOV BL, b[0] ; Also Copies 48h to BL
+#+END_SRC
+You can also use any of the memory index registers BX, SI, DI, BP, for example:
+#+BEGIN_SRC asm
+MOV SI, 3
+MOV AL, a[SI]
+#+END_SRC
+
+If you need to declare a large array you can use DUP operator.
+The syntax for *DUP*:
+
+number DUP ( value(s) )
+*number* - number of duplicate to make (any constant value).
+*value* - expression that DUP will duplicate.
+
+for example:
+#+BEGIN_SRC asm
+c DB 5 DUP(9)
+;is an alternative way of declaring:
+c DB 9, 9, 9, 9, 9
+#+END_SRC
+one more example:
+#+BEGIN_SRC asm
+d DB 5 DUP(1, 2)
+;is an alternative way of declaring:
+d DB 1, 2, 1, 2, 1, 2, 1, 2, 1, 2
+#+END_SRC
+Of course, you can use DW instead of DB if it's required to keep values larger then 255, or smaller then -128. DW cannot be used to declare strings.
; 2016-02-16 17:51:34 -0800 2664 - 'hash' any type' href='/akkartik/mu/commit/077hash.cc?h=hlt&id=37421f8a8bbccc871a3d452d81f03ad9a58f23bf'>37421f8a ^
1ead3562 ^
37421f8a ^













1ead3562 ^
37421f8a ^

1ead3562 ^
37421f8a ^

1ead3562 ^
37421f8a ^




37e71785 ^




1ead3562 ^
37e71785 ^

1ead3562 ^
37e71785 ^

1ead3562 ^
37e71785 ^




37421f8a ^



1ead3562 ^
37421f8a ^







1ead3562 ^
37421f8a ^






1ead3562 ^
37421f8a ^




1ead3562 ^
37421f8a ^

1ead3562 ^
37421f8a ^




1ead3562 ^
37421f8a ^











1ead3562 ^
37421f8a ^


1ead3562 ^
37421f8a ^


1ead3562 ^
37421f8a ^













1ead3562 ^
37421f8a ^

1ead3562 ^
37421f8a ^

1ead3562 ^
37421f8a ^










1ead3562 ^
37421f8a ^



1ead3562 ^
37421f8a ^



1ead3562 ^
37421f8a ^














1ead3562 ^
37421f8a ^

1ead3562 ^
37421f8a ^

1ead3562 ^
37421f8a ^













1ead3562 ^
37421f8a ^

1ead3562 ^
37421f8a ^

1ead3562 ^
37421f8a ^









1ead3562 ^
37421f8a ^


bff0fa45 ^
37421f8a ^









1b76245c ^
37421f8a ^

d5fe534d ^
1b76245c ^
d5fe534d ^




37421f8a ^
d5fe534d ^
















1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392









                                                                              






                                             
                                                                                                                                    

          













                                                                                       
























                                                                          
                                 

















                                                                
                   






















                                                                                         
                                                           


                                       
                                                













                                                                
                                                       
                                               
                                                                                                                 




















                                              
          

                         
                        

                         
                        













                                                        
          

                            
                        

                            
                        




                                          




                                              
          

                         
                        

                         
                        




                                         



                                                                       
          







                                                                            
          






                                        
          




                                         
                        

                                                   
                        




                                       
          











                                                                                      
          


                                            
                        


                                                         
                        













                                                   
          

                         
                        

                         
                        










                                                            
          



                                                  
                        



                                                  
                         














                                                                   
          

                                 
                        

                                 
                         













                                                                   
          

                            
                        

                        
                        









                                                                       
          


                                                       
                                       









                                             
                                                                                                                                        

          
                                              
                                                                                                                                                                                              




                                                
                
















                                                         
// A universal hash function that can handle objects of any type.
//
// The way it's currently implemented, two objects will have the same hash if
// all their non-address fields (all the way down) expand to the same sequence
// of scalar values. In particular, a container with all zero addresses hashes
// to 0. Hopefully this won't be an issue because we are usually hashing
// objects of a single type in any given hash table.
//
// Based on http://burtleburtle.net/bob/hash/hashfaq.html

:(before "End Primitive Recipe Declarations")
HASH,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "hash", HASH);
:(before "End Primitive Recipe Checks")
case HASH: {
  if (SIZE(inst.ingredients) != 1) {
    raise << maybe(get(Recipe, r).name) << "'hash' takes exactly one ingredient rather than '" << to_string(inst) << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case HASH: {
  reagent input = current_instruction().ingredients.at(0);  // copy
  products.resize(1);
  products.at(0).push_back(hash(0, input));
  break;
}

//: in all the code below, the intermediate results of hashing are threaded through 'h'

:(code)
size_t hash(size_t h, reagent& r) {
  canonize(r);
  if (is_mu_string(r))  // optimization
    return hash_mu_string(h, r);
  else if (is_mu_address(r))
    return hash_mu_address(h, r);
  else if (is_mu_scalar(r))
    return hash_mu_scalar(h, r);
  else if (is_mu_array(r))
    return hash_mu_array(h, r);
  else if (is_mu_container(r))
    return hash_mu_container(h, r);
  else if (is_mu_exclusive_container(r))
    return hash_mu_exclusive_container(h, r);
  assert(false);
}

size_t hash_mu_scalar(size_t h, const reagent& r) {
  double input = is_literal(r) ? r.value : get_or_insert(Memory, r.value);
  return hash_iter(h, static_cast<size_t>(input));
}

size_t hash_mu_address(size_t h, reagent& r) {
  if (r.value == 0) return 0;
  r.value = get_or_insert(Memory, r.value);
  drop_from_type(r, "address");
  if (r.type->name == "shared") {
    ++r.value;
    drop_from_type(r, "shared");
  }
  return hash(h, r);
}

size_t hash_mu_string(size_t h, const reagent& r) {
  string input = read_mu_string(get_or_insert(Memory, r.value));
  for (long long int i = 0; i < SIZE(input); ++i) {
    h = hash_iter(h, static_cast<size_t>(input.at(i)));
//?     cerr << i << ": " << h << '\n';
  }
  return h;
}

size_t hash_mu_array(size_t h, const reagent& r) {
  long long int size = get_or_insert(Memory, r.value);
  reagent elem = r;
  delete elem.type;
  elem.type = new type_tree(*array_element(r.type));
  for (long long int i=0, address = r.value+1; i < size; ++i, address += size_of(elem)) {
    reagent tmp = elem;
    tmp.value = address;
    h = hash(h, tmp);
//?     cerr << i << " (" << address << "): " << h << '\n';
  }
  return h;
}

bool is_mu_container(const reagent& r) {
  if (r.type->value == 0) return false;
  type_info& info = get(Type, r.type->value);
  return info.kind == CONTAINER;
}

size_t hash_mu_container(size_t h, const reagent& r) {
  assert(r.type->value);
  type_info& info = get(Type, r.type->value);
  long long int address = r.value;
  long long int offset = 0;
  for (long long int i = 0; i < SIZE(info.elements); ++i) {
    reagent element = element_type(r, i);
    if (has_property(element, "ignore-for-hash")) continue;
    element.set_value(address+offset);
    h = hash(h, element);
//?     cerr << i << ": " << h << '\n';
    offset += size_of(info.elements.at(i).type);
  }
  return h;
}

bool is_mu_exclusive_container(const reagent& r) {
  if (r.type->value == 0) return false;
  type_info& info = get(Type, r.type->value);
  return info.kind == EXCLUSIVE_CONTAINER;
}

size_t hash_mu_exclusive_container(size_t h, const reagent& r) {
  assert(r.type->value);
  long long int tag = get(Memory, r.value);
  reagent variant = variant_type(r, tag);
  // todo: move this error to container definition time
  if (has_property(variant, "ignore-for-hash"))
    raise << get(Type, r.type->value).name << ": /ignore-for-hash won't work in exclusive containers\n" << end();
  variant.set_value(r.value + /*skip tag*/1);
  h = hash(h, variant);
  return h;
}

size_t hash_iter(size_t h, size_t input) {
  h += input;
  h += (h<<10);
  h ^= (h>>6);

  h += (h<<3);
  h ^= (h>>11);
  h += (h<<15);
  return h;
}

:(scenario hash_container_checks_all_elements)
container foo [
  x:number
  y:character
]
def main [
  1:foo <- merge 34, 97/a
  3:number <- hash 1:foo
  return-unless 3:number
  4:foo <- merge 34, 98/a
  6:number <- hash 4:foo
  return-unless 6:number
  7:boolean <- equal 3:number, 6:number
]
# hash on containers includes all elements
+mem: storing 0 in location 7

:(scenario hash_exclusive_container_checks_all_elements)
exclusive-container foo [
  x:bar
  y:number
]
container bar [
  a:number
  b:number
]
def main [
  1:foo <- merge 0/x, 34, 35
  4:number <- hash 1:foo
  return-unless 4:number
  5:foo <- merge 0/x, 34, 36
  8:number <- hash 5:foo
  return-unless 8:number
  9:boolean <- equal 4:number, 8:number
]
# hash on containers includes all elements
+mem: storing 0 in location 9

:(scenario hash_can_ignore_container_elements)
container foo [
  x:number
  y:character/ignore-for-hash
]
def main [
  1:foo <- merge 34, 97/a
  3:number <- hash 1:foo
  return-unless 3:number
  4:foo <- merge 34, 98/a
  6:number <- hash 4:foo
  return-unless 6:number
  7:boolean <- equal 3:number, 6:number
]
# hashes match even though y is different
+mem: storing 1 in location 7

//: These properties aren't necessary for hash, they just test that the
//: current implementation works like we think it does.

:(scenario hash_of_zero_address)
def main [
  1:address:number <- copy 0
  2:number <- hash 1:address:number
]
+mem: storing 0 in location 2

//: This is probably too aggressive, but we need some way to avoid depending
//: on the precise bit pattern of a floating-point number.
:(scenario hash_of_numbers_ignores_fractional_part)
def main [
  1:number <- hash 1.5
  2:number <- hash 1
  3:boolean <- equal 1:number, 2:number
]
+mem: storing 1 in location 3

:(scenario hash_of_array_same_as_string)
def main [
  10:number <- copy 3
  11:number <- copy 97
  12:number <- copy 98
  13:number <- copy 99
  2:number <- hash 10:array:number/unsafe
  return-unless 2:number
  3:address:shared:array:character <- new [abc]
  4:number <- hash 3:address:shared:array:character
  return-unless 4:number
  5:boolean <- equal 2:number, 4:number
]
+mem: storing 1 in location 5

:(scenario hash_ignores_address_value)
def main [
  1:address:shared:number <- new number:type
  *1:address:shared:number <- copy 34
  2:number <- hash 1:address:shared:number
  3:address:shared:number <- new number:type
  *3:address:shared:number <- copy 34
  4:number <- hash 3:address:shared:number
  5:boolean <- equal 2:number, 4:number
]
# different addresses hash to the same result as long as the values the point to do so
+mem: storing 1 in location 5

:(scenario hash_ignores_address_refcount)
def main [
  1:address:shared:number <- new number:type
  *1:address:shared:number <- copy 34
  2:number <- hash 1:address:shared:number
  return-unless 2:number
  # increment refcount
  3:address:shared:number <- copy 1:address:shared:number
  4:number <- hash 3:address:shared:number
  return-unless 4:number
  5:boolean <- equal 2:number, 4:number
]
# hash doesn't change when refcount changes
+mem: storing 1 in location 5

:(scenario hash_container_depends_only_on_elements)
container foo [
  x:number
  y:character
]
container bar [
  x:number
  y:character
]
def main [
  1:foo <- merge 34, 97/a
  3:number <- hash 1:foo
  return-unless 3:number
  4:bar <- merge 34, 97/a
  6:number <- hash 4:bar
  return-unless 6:number
  7:boolean <- equal 3:number, 6:number
]
# containers with identical elements return identical hashes
+mem: storing 1 in location 7

:(scenario hash_container_depends_only_on_elements_2)
container foo [
  x:number
  y:character
  z:address:shared:number
]
def main [
  1:address:shared:number <- new number:type
  *1:address:shared:number <- copy 34
  2:foo <- merge 34, 97/a, 1:address:shared:number
  5:number <- hash 2:foo
  return-unless 5:number
  6:address:shared:number <- new number:type
  *6:address:shared:number <- copy 34
  7:foo <- merge 34, 97/a, 6:address:shared:number
  10:number <- hash 7:foo
  return-unless 10:number
  11:boolean <- equal 5:number, 10:number
]
# containers with identical 'leaf' elements return identical hashes
+mem: storing 1 in location 11

:(scenario hash_container_depends_only_on_elements_3)
container foo [
  x:number
  y:character
  z:bar
]
container bar [
  x:number
  y:number
]
def main [
  1:foo <- merge 34, 97/a, 47, 48
  6:number <- hash 1:foo
  return-unless 6:number
  7:foo <- merge 34, 97/a, 47, 48
  12:number <- hash 7:foo
  return-unless 12:number
  13:boolean <- equal 6:number, 12:number
]
# containers with identical 'leaf' elements return identical hashes
+mem: storing 1 in location 13

:(scenario hash_exclusive_container_ignores_tag)
exclusive-container foo [
  x:bar
  y:number
]
container bar [
  a:number
  b:number
]
def main [
  1:foo <- merge 0/x, 34, 35
  4:number <- hash 1:foo
  return-unless 4:number
  5:bar <- merge 34, 35
  7:number <- hash 5:bar
  return-unless 7:number
  8:boolean <- equal 4:number, 7:number
]
# hash on containers includes all elements
+mem: storing 1 in location 8

//: An older version that supported only strings.
//: Hash functions are subtle and easy to get wrong, so we keep the old
//: version around and check that the new one is consistent with it.

:(scenario hash_matches_old_version)
def main [
  1:address:shared:array:character <- new [abc]
  2:number <- hash 1:address:shared:array:character
  3:number <- hash_old 1:address:shared:array:character
  4:boolean <- equal 2:number, 3:number
]
+mem: storing 1 in location 4

:(before "End Primitive Recipe Declarations")
HASH_OLD,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "hash_old", HASH_OLD);
:(before "End Primitive Recipe Checks")
case HASH_OLD: {
  if (SIZE(inst.ingredients) != 1) {
    raise << maybe(get(Recipe, r).name) << "'hash_old' takes exactly one ingredient rather than '" << to_string(inst) << "'\n" << end();
    break;
  }
  if (!is_mu_string(inst.ingredients.at(0))) {
    raise << maybe(get(Recipe, r).name) << "'hash_old' currently only supports strings (address:shared:array:character), but got " << inst.ingredients.at(0).original_string << '\n' << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case HASH_OLD: {
  string input = read_mu_string(ingredients.at(0).at(0));
  size_t h = 0 ;

  for (long long int i = 0; i < SIZE(input); ++i) {
    h += static_cast<size_t>(input.at(i));
    h += (h<<10);
    h ^= (h>>6);

    h += (h<<3);
    h ^= (h>>11);
    h += (h<<15);
  }

  products.resize(1);
  products.at(0).push_back(h);
  break;
}