forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
07-ibcm.html
842 lines (763 loc) · 29.1 KB
/
07-ibcm.html
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CS 2150: 07-ibcm slide set</title>
<meta name="description" content="A set of slides for a course on Program and Data Representation">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="../slides/reveal.js/dist/reset.css">
<link rel="stylesheet" href="../slides/reveal.js/dist/reveal.css">
<link rel="stylesheet" href="../slides/reveal.js/dist/theme/black.css" id="theme">
<link rel="stylesheet" href="../slides/css/pdr.css">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="../slides/reveal.js/plugin/highlight/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? '../slides/reveal.js/css/print/pdf.scss' : '../slides/reveal.js/css/print/paper.scss';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="../slides/reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
<style>.reveal li { font-size:93%; line-height:120%; }</style>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section data-markdown id="cover"><script type="text/template">
# CS 2150
### Program and Data Representation
<p class='titlep'> </p>
<div class="titlesmall"><p>
<a href="http://www.cs.virginia.edu/~mrf8t">Mark Floryan</a> ([email protected])<br>
<a href="http://www.cs.virginia.edu/~asb">Aaron Bloomfield</a> ([email protected])<br>
<a href="http://github.com/uva-cs/pdr">@github</a> | <a href="index.html">↑</a> | <a href="./07-ibcm.html?print-pdf"><img class="print" width="20" src="../slides/images/print-icon.png" style="top:0px;vertical-align:middle"></a>
</p></div>
<p class='titlep'> </p>
## IBCM (machine language)
</script></section>
<section>
<h2>CS 2150 Roadmap</h2>
<table class="wide">
<tr><td colspan="3"><p class="center">Data Representation</p></td><td></td><td colspan="3"><p class="center">Program Representation</p></td></tr>
<tr>
<td class="top"><small> <br> <br>string<br> <br> <br> <br>int x[3]<br> <br> <br> <br>char x<br> <br> <br> <br>0x9cd0f0ad<br> <br> <br> <br>01101011</small></td>
<!-- image adapted from http://openclipart.org/detail/3677/arrow-left-right-by-torfnase -->
<td><img class="noborder" src="images/red-double-arrow.png" height="500" alt="vertical red double arrow"></td>
<td class="top"> <br>Objects<br> <br>Arrays<br> <br>Primitive types<br> <br>Addresses<br> <br>bits</td>
<td> </td>
<td class="top"><small> <br> <br>Java code<br> <br> <br>C++ code<br> <br> <br>C code<br> <br> <br>x86 code<br> <br> <br>IBCM<br> <br> <br>hexadecimal</small></td>
<!-- image adapted from http://openclipart.org/detail/3677/arrow-left-right-by-torfnase -->
<td><img class="noborder" src="images/green-double-arrow.png" height="500" alt="vertical green double arrow"></td>
<td class="top"> <br>High-level language<br> <br>Low-level language<br> <br>Assembly language<br> <br>Machine code</td>
</tr>
</table>
</section>
<section data-markdown><script type="text/template">
# Contents
[Introduction](#/introduction)
[IBCM Description](#/ibcmdesc)
[Writing IBCM code](#/writingibcm)
[Conclusions](#/conclusions)
</script></section>
<section>
<section id="introduction" data-markdown class="center"><script type="text/template">
# Introduction
</script></section>
<section data-markdown><script type="text/template">
## Assembly Language
- Machine language
- Instructions represented as patterns of bits (0s and 1s) that can be understood and processed by a central processing unit (CPU)
- Program stored in main memory
- Assembly language
- Human-readable notation for the machine language used to control a specific computer architecture
- Assembler translates to bits
</script></section>
<section data-markdown><script type="text/template">
## Why Learn Assembly Language?
- Machine designers
- Compiler writers
- Programmers (especially for OSes)
- Assembly language programmers
- Most importantly: it helps you understand how computers compute
</script></section>
<section>
<h2>Memory Hierarchy, part 1</h2>
<table class="transparent"><tr><td class="top">
<img src="images/07-ibcm/memory-hierarchy.png" alt="memory hierarchy">
</td><td>
<ul>
<li>CPU registers<ul>
<li>1 access per CPU cycle</li>
<li>3*10<sup>9</sup> accesses per sec</li>
<li>1 Kb total storage</li></ul></li>
<li>Cache<ul>
<li>SDRAM: 10 ns</li>
<li>10<sup>8</sup> accesses per sec</li>
<li>Multiple levels possible</li>
<li>Higher levels are bigger and slower</li>
<li>1 Mb total storage</li></ul></li>
</ul>
</td>
</tr></table>
</section>
<section>
<h2>Memory Hierarchy, part 2</h2>
<table class="transparent"><tr><td class="top">
<img src="images/07-ibcm/memory-hierarchy.png" alt="memory hierarchy">
</td><td class="top">
<ul>
<li>Main memory<ul>
<li>DRAM: 60 ns</li>
<li>2*10<sup>7</sup> accesses per sec</li>
<li>Limited by bus speeds</li>
<li>1 Gb total storage</li></ul></li>
<li>Disk<ul>
<li>HDD speeds: 5 ms</li>
<li>200 accesses per sec</li>
<li>1 Tb total storage</li></ul></li>
</ul>
</td></tr></table>
</section>
<section data-markdown><script type="text/template">
## Fetch Execute Cycle
```
while(power is on) {
IR := memory[PC]
Increment PC by length of instruction
execute instruction in IR
}
```
- PC = program counter
- IR = instruction register
</script></section>
<section data-markdown><script type="text/template">
## Assembly Language Instructions
- x86
```
add eax, ebx
add ecx, 1
```
- Explicit use of registers<br> <br>
- IBCM
```
load 100
add 200
store 300
```
- Implied use of accumulator
</script></section>
</section>
<section>
<section id="ibcmdesc" data-markdown class="center"><script type="text/template">
# IBCM Description
</script></section>
<section data-markdown><script type="text/template">
## Running IBCM programs
- Online IBCM simulator
- https://andromeda.cs.virginia.edu/ibcm/
- https://libra.cs.virginia.edu/ibcm/
- This program will hang your browser if it gets stuck in an infinite loop, due to how web browsers (don't) handle Javascript threads and polling of web pages
</script></section>
<section data-markdown><script type="text/template">
## IBCM Machine Description: CPU
- Single accumulator
- 16 bits
- Special purpose registers
- IR: instruction register
- Stores bits which encode instruction
- PC: program counter
- Stores an address of an instruction
</script></section>
<section>
<h2>IBCM Machine: Memory</h2>
<table class="transparent">
<tr><td class="middle"><ul>
<li>4096 16-bit words<ul>
<li>Word<ul>
<li>"chunk size" or addressable unit</li></ul></li>
<li>All initialized to zero initially<ul>
<li>Unlike C/C++</li></ul></li></ul></li>
</ul>
</td><td><img src="images/07-ibcm/ibcm-memory.png" alt="ibcm memory diagram"></td></tr></table>
</section>
<section>
<h2>IBCM instruction format</h2>
<table class="transparent" style="border-collapse:collapse">
<tr>
<td> 15</td>
<td>14</td>
<td>13</td>
<td>12</td>
<td>11 10</td>
<td style="width:300px">...</td>
<td style="text-align:right">0</td>
<td></td>
</tr>
<tr>
<td style="border-top:1px solid; border-left:1px solid" class="middle"> 0</td>
<td style="border-top:1px solid" class="middle">0</td>
<td style="border-top:1px solid" class="middle">0</td>
<td style="border-top:1px solid; border-right:1px solid" class="middle">0</td>
<td style="border-top:1px solid"> <br> </td>
<td style="border-top:1px solid; border-bottom:1px solid" class="middle">(unused)</td>
<td style="border-top:1px solid; border-right:1px solid; border-bottom:1px solid"> </td>
<td class="middle">halt</td>
</tr>
<tr>
<td style="border-top:1px solid; border-left:1px solid" class="middle"> 0</td>
<td style="border-top:1px solid" class="middle">0</td>
<td style="border-top:1px solid" class="middle">0</td>
<td style="border-top:1px solid" class="middle">1</td>
<td style="border:1px solid">I/O<br>op</td>
<td class="middle">(unused)</td>
<td style="border-right:1px solid"> </td>
<td class="middle">I/O</td>
</tr>
<tr>
<td style="border-top:1px solid; border-left:1px solid" class="middle"> 0</td>
<td style="border-top:1px solid" class="middle">0</td>
<td style="border-top:1px solid" class="middle">1</td>
<td style="border-top:1px solid" class="middle">0</td>
<td style="border:1px solid"> shift<br>op</td>
<td style="border:1px solid" class="middle">(unused)</td>
<td style="border:1px solid" class="middle"> count</td>
<td class="middle">shifts</td>
</tr>
<tr>
<td colspan="4" style="border:1px solid" class="middle"> opcode</td>
<td style="border-bottom:1px solid"> </td>
<td style="border-bottom:1px solid" class="middle">address</td>
<td style="border-bottom:1px solid; border-right:1px solid"> <br> </td>
<td class="middle"> others</td>
</tr>
</table>
</section>
<section data-markdown><script type="text/template">
## IBCM instruction types
- Halt
- Opcode is 0
- I/O
- Opcode is 1
- next 2 bits specify I/O type
- Shifts
- Opcode is 2
- next 2 bits specify shift type
- last 4 bits specify shift amount
- Others
- Opcode is 3 to F (15)
- Last 12 bits specify the address for the instruction
- The arithmetic, memory, and control instructions
</script></section>
<section data-markdown><script type="text/template">
## Halt
- The opcode (bits 15 down to 12) is zero
- It doesn't matter what the next 12 bits are<br>
- Not much else to say here...<br>
- It halts the IBCM!
</script></section>
<section data-markdown><script type="text/template">
## Input and output
- The opcode (bits 15 down to 12) is 1
- Next two bits specify I/O type:
- Bit 11 specifies input (0) or output (1)
- Bit 10 specifies hex word (0) or ascii (1)
- Combinations:
- 00: read hex word from keyboard
- 01: read ascii character (into acc bits 7-0)
- 10: write hex word to screen
- 11: write ascii character (from acc bits 7-0)
</script></section>
<section data-markdown><script type="text/template">
## Shifts
- The opcode (bits 15 down to 12) is 2
- Next two bits specify shift type:
- Bit 11 specifies shift (0) or rotate (1)
- Bit 10 specifies direction: left (0) or right (1)
- Consider the bits <span class="skyblue">0000</span> <span class="yellow">1111</span> <span class="pink">0000</span> <span class="lightgreen">1111</span> and a 3-bit shift/rotation:
- 00: shift left: <span class="skyblue">0</span><span class="yellow">111 1</span><span class="pink">000 0</span><span class="lightgreen">111 1</span><span class="red">000</span>
- 01: shift right: <span class="red">000</span><span class="skyblue">0 000</span><span class="yellow">1 111</span><span class="pink">0 000</span><span class="lightgreen">1</span>
- 10: rotate left: <span class="skyblue">0</span><span class="yellow">111 1</span><span class="pink">000 0</span><span class="lightgreen">111 1</span><span class="skyblue">000</span>
- 11: rotate right: <span class="lightgreen">111</span><span class="skyblue">0 000</span><span class="yellow">1 111</span><span class="pink">0 000</span><span class="lightgreen">1</span>
</script></section>
<section data-markdown><script type="text/template">
## Other instructions
- Opcode (bits 15 down to 12) varies from 3 to F (15)
- Next 12 bits specifies the address
- See next slide for a list...
</script></section>
<section>
<h2>IBCM "other" instructions</h2>
<table style="font-size:80%;line-height:120%;">
<tr><th>Op</th><th>Name</th><th>HLL meaning</th><th>Description</th></tr>
<tr><td>3</td><td>load</td><td><code>a := mem[addr]</code></td><td>load accumulator from memory</td></tr>
<tr><td>4</td><td>store</td><td><code>mem[addr] := a</code></td><td>store accumulator into memory</td></tr>
<tr><td>5</td><td>add</td><td><code>a := a + mem[addr]</code></td><td>add memory to accumulator</td></tr>
<tr><td>6</td><td>sub</td><td><code>a := a - mem[addr]</code></td><td>subtract memory from accumulator</td></tr>
<tr><td>7</td><td>and</td><td><code>a := a ∧ mem[addr]</code></td><td>logical 'and' memory into accumulator</td></tr>
<tr><td>8</td><td>or</td><td><code>a := a ∨ mem[addr]</code></td><td>logical 'or' memory into accumulator</td></tr>
<tr><td>9</td><td>xor</td><td><code>a := a ⊕ mem[addr]</code></td><td>logical 'xor' memory into accumulator</td></tr>
<tr><td>A</td><td>not</td><td><code>a := ~a</code></td><td>logical complement of accumulator</td></tr>
<tr><td>B</td><td>nop</td><td><code>/* */</code></td><td>do nothing (no operation)</td></tr>
<tr><td>C</td><td>jmp</td><td><code>goto addr</code></td><td>jump to 'addr'</td></tr>
<tr><td>D</td><td>jmpe</td><td><code>if a == 0 goto addr</code></td><td>jump to 'addr' if accumulator == zero</td></tr>
<tr><td>E</td><td>jmpl</td><td><code>if a < 0 goto addr</code></td><td>jump to 'addr' if accum. is < zero</td></tr>
<tr><td>F</td><td>brl</td><td><code>a := PC+1; goto addr</code></td><td>jump (branch) to 'addr'; set accum. to<br>PC+1 (next inst) and jump</td></tr>
</table>
</section>
<section data-markdown><script type="text/template">
## Labels
- We often need to jump around our code
- For this we use labels in assembly language
- These are translated into absolute memory addresses later by assembler
- We don't have an assembler in IBCM, so we will have to translate them ourselves
- Examples:
```
start readH
loop load n
xit load s
```
</script></section>
<section data-markdown><script type="text/template">
## Declaring variables
- You use the `dw` opcode
- It stands for Declare Word, perhaps
- And you need to give it a label, so you can reference it in assembly language program
- Example:
- `n dw 0` declares a spot with label 'n' to have value 0
- `load n` will load the value stored in that spot in memory
</script></section>
<section>
<h2>IBCM Machine</h2>
<table class="transparent">
<tr><td>
<table class="transparent">
<tr><td>Address</td><td>Contents</td><td>Command</td></tr>
<tr><td>000</td><td class="border">0000</td><td>halt</td></tr>
<tr><td>001</td><td class="border">000f</td><td>halt</td></tr>
<tr><td>002</td><td class="border">0005</td><td>halt</td></tr>
<tr><td>003</td><td class="border">3001</td><td>load mem[1]</td></tr>
<tr><td>004</td><td class="border">5002</td><td>add mem[2]</td></tr>
<tr><td>005</td><td class="border" style="border-bottom:medium solid;">0000</td><td>halt</td></tr>
</table>
</td><td style="width:100px"></td><td>
<table class="transparent">
<tr><td> </td><td></td></tr>
<tr><td>PC</td><td class="border" style="width:100px;border-bottom:medium solid;"> </td></tr>
<tr><td></td><td> </td></tr>
<tr><td>IR</td><td class="border" style="border-bottom:medium solid;"></td></tr>
<tr><td></td><td> </td></tr>
<tr><td>Accum</td><td class="border" style="border-bottom:medium solid;"></td></tr>
</table>
</td></tr></table>
</section>
<section>
<h2>Sample Program</h2>
<table class="transparent">
<tr><td>
<table class="transparent">
<tr><td>Address</td><td>Contents</td><td>Command</td></tr>
<tr><td>000</td><td class="border">3000</td><td class="fragment">load mem[0]</td></tr>
<tr><td>001</td><td class="border">5000</td><td class="fragment">add mem[0]</td></tr>
<tr><td>002</td><td class="border">6001</td><td class="fragment">sub mem[1]</td></tr>
<tr><td>003</td><td class="border">8003</td><td class="fragment">or mem[3]</td></tr>
<tr><td>004</td><td class="border">a000</td><td class="fragment">not</td></tr>
<tr><td>005</td><td class="border">4000</td><td class="fragment">store mem[0]</td></tr>
<tr><td>006</td><td class="border" style="border-bottom:medium solid;">f000</td><td class="fragment">brl mem[0]</td></tr>
</table>
</td><td style="width:100px"></td><td>
<table class="transparent">
<tr><td> </td><td></td></tr>
<tr><td> </td><td></td></tr>
<tr><td>PC</td><td class="border" style="width:100px;border-bottom:medium solid;"> </td></tr>
<tr><td></td><td> </td></tr>
<tr><td>IR</td><td class="border" style="border-bottom:medium solid;"></td></tr>
<tr><td></td><td> </td></tr>
<tr><td>Accum</td><td class="border" style="border-bottom:medium solid;"></td></tr>
</table>
</td></tr></table>
</section>
<section data-markdown><script type="text/template">
## What It Does
- 0x3000 is a *load* of address 0 into the accum (accum is 3000)
- 0x5000 is a *add* of address 0 to the accum (accum is 6000)
- 0x6001 is a *subtract* of address 1 from the accum (accum is 1000)
- 0x8003 is an *or* of the accum with memory address 3 (accum is 9003)
- 0xA000 is a *not* of the accum (accum is 6FFC)
- 0x4000 is a *store* of the accum into memory 0
- 0xF000 is a *branch and link*
- the PC+1 (0x0007) is stored in the accum
- goto address 0
- Address 0 is now 6FFC
- we've modified our binary code!
- that's a subtract of memory address FFC from the accum
- all memory is initialized to zero, so mem[ffc] = 0
- 0x0007 -- 0x0000 = 0x0007
</script></section>
</section>
<section>
<section id="writingibcm" data-markdown class="center"><script type="text/template">
# Writing IBCM Code
</script></section>
<section data-markdown><script type="text/template">
## Writing IBCM Code
- Write high level pseudo-code
```
for ( i = 1; i < max; i++ ) ...
```
- Translate into IBCM assembly instructions
```
load one
store i
```
- Test code by hand
- (step through the code)
- Encode into machine code
```
3016
4005
```
- Load machine code into simulator and run!
</script></section>
<section data-markdown><script type="text/template">
## IBCM Code to compute a sum
- Compute the sum of integers 1 through *n*, where *n* is to be read from the keyboard
- The resulting sum is to be printed to the screen
- Halt after printing the sum
- Source code: [summation.ibcm](../ibcm/summation.ibcm)
- Pseudocode:
```
read n;
i = 1; // index in the array
s = 0; // ongoing sum
while (i <= n) {
s += i;
i += 1;
}
print s;
```
</script></section>
<section data-markdown><script type="text/template">
## IBCM summation program
```
mem locn label op addr comments
C00A 000 jmp start skip around the variables
0000 001 i dw 0 int i
0000 002 s dw 0 int s
0000 003 n dw 0 int n
0001 004 one dw 1
0000 005 zero dw 0
0000 006 leave space for changes
0000 007
0000 008
0000 009
1000 00A start readH read n
4003 00B store n
3004 00C load one i = 1
4001 00D store i
3005 00E load zero s = 0
4002 00F store s
3003 010 loop load n if (i > n) goto xit
6001 011 sub i
E01A 012 jmpl xit
3002 013 load s s += i
5001 014 add i
4002 015 store s
3001 016 load i i += 1
5004 017 add one
4001 018 store i
C010 019 jmp loop goto loop
3002 01A xit load s print s
1800 01B printH
0000 01C halt halt
```
We'll look at this code in parts...
</script></section>
<section data-markdown><script type="text/template">
## IBCM summation program: part 1
```
mem locn label op addr comments
C00A 000 jmp start skip around the vars
0000 001 i dw 0 int i
0000 002 s dw 0 int s
0000 003 n dw 0 int n
0001 004 one dw 1
0000 005 zero dw 0
...
1000 00A start readH read n
4003 00B store n
3004 00C load one i = 1
4001 00D store i
3005 00E load zero s = 0
4002 00F store s
```
</script></section>
<section data-markdown><script type="text/template">
## IBCM summation program: part 2
```
mem locn label op addr comments
3003 010 loop load n if (i > n) goto xit
6001 011 sub i
E01A 012 jmpl xit
3002 013 load s s += i
5001 014 add i
4002 015 store s
3001 016 load i i += 1
5004 017 add one
4001 018 store i
C010 019 jmp loop goto loop
3002 01A xit load s print s
1800 01B printH
0000 01C halt halt
```
</script></section>
<section>
<h2>How Would You Code This?</h2>
<pre><code>if (B == 0)
S1;
else
S2;
</code></pre>
</section>
<section>
<h2>How Would You Code This?</h2>
<pre><code>while(B >= 5)
S;
</code></pre>
</section>
<section data-markdown><script type="text/template">
## IBCM Code to Sum Elements in an Array
- Compute the sum of the elements of an array and print this sum on the screen (then halt)
- The address of the first element of the array and the size of the array are to be read in from the keyboard
- Pseudocode:
```
read a; // array base address
read n; // array size
i = 0; // index in the array
s = 0; // ongoing sum
while (i < n) {
s += a[i];
i += 1;
}
print s;
```
- Source code: [array-summation.ibcm](../ibcm/array-summation.ibcm)
</script></section>
<section data-markdown><script type="text/template">
## IBCM Code to Sum Elements in an Array
```
mem locn label op addr comments
C00A 000 jmp start skip around the variables
0000 001 i dw 0 int i
0000 002 s dw 0 int s
0000 003 a dw 0 int a[]
0000 004 n dw 0
0000 005 zero dw 0
0001 006 one dw 1
5000 007 adit dw 5000
0000 008 leave space for changes
0000 009
1000 00A start readH read array address
4003 00B store a
1000 00C readH read array size
4004 00D store n
3005 00E load zero i = 0; s = 0;
4001 00F store i
4002 010 store s
3004 011 loop load n if (i >= N) goto xit
6001 012 sub i
E020 013 jmpl xit
D020 014 jmpe xit
3007 015 load adit form the inst to add a[i]
5003 016 add a
5001 017 add i
401A 018 store doit plant inst into the code
3002 019 load s s += a[i]
0000 01A doit dw 0
4002 01B store s
3001 01C load i i += 1
5006 01D add one
4001 01E store i
C011 01F jmp loop goto loop
3002 020 xit load s print s
1800 021 printH
0000 022 halt
```
We'll look at this code in parts...
</script></section>
<section data-markdown><script type="text/template">
## Array summation: initialization and halting
```
mem locn label op addr comments
C00A 000 jmp start skip around the vars
0000 001 i dw 0 int i
0000 002 s dw 0 int s
0000 003 a dw 0 int a[]
0000 004 n dw 0
0000 005 zero dw 0
0001 006 one dw 1
5000 007 adit dw 5000
... leave space for changes
1000 00A start readH read array address
4003 00B store a
1000 00C readH read array size
4004 00D store n
3005 00E load zero i = 0; s = 0;
4001 00F store i
4002 010 store s
...
3002 020 xit load s print s
1800 021 printH
0000 022 halt
```
</script></section>
<section data-markdown><script type="text/template">
## Array summation: the inner loop
```
mem locn label op addr comments
...
3004 011 loop load n if (i >= N) goto xit
6001 012 sub i
E020 013 jmpl xit
D020 014 jmpe xit
3007 015 load adit form inst to add a[i]
5003 016 add a
5001 017 add i
401A 018 store doit plant inst into code
3002 019 load s s += a[i]
0000 01A doit dw 0
4002 01B store s
3001 01C load i i += 1
5006 01D add one
4001 01E store i
C011 01F jmp loop goto loop
...
```
</script></section>
</section>
<section>
<section id="conclusions" data-markdown class="center"><script type="text/template">
# Conclusions
</script></section>
<section data-markdown><script type="text/template">
## Notes
- IBCM's memory
- Array of "words" / chunks of data
- Data can be the program
- Program can be the data
- IBCM is Turing-complete
- Any program expressible in any programming language can be expressed in IBCM
- (with the standard exception about the whole limited-memory thing)
</script></section>
<section data-markdown><script type="text/template">
## IBCM Tips/Reminders
- Use the programming steps
1. Pseudocode
2. Assembly code
- Comment your code clearly!
3. Trace the assembly
4. Translate to machine code... LAST!!!
5. Run in the simulator to verify that it works
- Cannot have blank lines (or comment lines)
</script></section>
<section data-markdown><script type="text/template">
## Use the simulators to debug your code
- Online simulator
- Cannot terminate a infinite loop easily
- Unusual behavior?
- Is the program logic correct?
- Condition specified incorrectly/inaccurately?
- Is the machine code correct?
- Opcode? Address?
</script></section>
<section>
<h2>What's missing from IBCM?</h2>
<ul class="fragment">
<li>Integer multiply and divide</li>
<li>Floating point support</li>
<li>A bigger address space</li>
<li>More than 1 user register</li>
<li>What else?</li>
</ul>
</section>
<section data-markdown><script type="text/template">
## Emulating IBCM in C++
- How might one write software to emulate an IBCM machine in C++?
- A switch statement with 16 cases, perhaps
- But how to decode the instructions?
</script></section>
<section data-markdown><script type="text/template">
## How to decode the parts of an instruction
- Let's assume we had to write a C++ program that could extract the parts of an IBCM instruction
- How to do it?
- Assume the instruction is in an `unsigned int x`
```
unsigned int opcode = (x >> 12) & 0x000f
unsigned int ioshiftop = (x >> 10) & 0x0003
unsigned int address = x & 0x0fff
unsigned int shiftcount = x & 0x000f
```
</script></section>
<section data-markdown><script type="text/template">
## What about encoding
- Assume we have (unsigned ints) opcode, ioshiftop and shiftcount
```
unsigned int instruction = (opcode << 12) |
(ioshiftop << 10) | shiftcount
```
- What a pain this all is!
</script></section>
<section data-markdown><script type="text/template">
## A data structure to make it easier
```
union ibcm_instruction {
#ifdef BIG_ENDIAN // the IBCM is big endian
struct { unsigned char high, low; } bytes;
#else
#ifdef LITTLE_ENDIAN
struct { unsigned char low, high; } bytes;
#else
#error Must define BIG_ENDIAN or LITTLE_ENDIAN
#endif // LITTLE_ENDIAN
#endif // BIG_ENDIAN
struct { unsigned int op:4, unused:12; } halt;
struct { unsigned int op:4, ioopt:2, unused:10 } io;
struct { unsigned int op:4, shiftop: 2,
unused:5, shiftcount:5; } shifts;
struct { unsigned int op:4, address:12; } others;
};
```
</script></section>
<section data-markdown><script type="text/template">
## Using that data structure
```
// read in instruction into unsigned chars a and b
ibcm_instruction inst;
inst.high = a;
inst.low = b;
if ( inst.halt.op == 0 ) { // halt
// ...
} else if ( inst.io.op == 1 ) { // io
cout << inst.io.ioopt << endl;
} else if ( inst.shifts.op == 2 ) { // shifts
cout << inst.shifts.shiftop << endl;
cout << inst.shifts.shiftcount << endl;
} else { // all others
cout << inst.others.address << endl;
}
```
</script></section>
</section>
</div>
</div>
<script src='../slides/reveal.js/dist/reveal.js'></script><script src='../slides/reveal.js/plugin/zoom/zoom.js'></script><script src='../slides/reveal.js/plugin/notes/notes.js'></script><script src='../slides/reveal.js/plugin/search/search.js'></script><script src='../slides/reveal.js/plugin/markdown/markdown.js'></script><script src='../slides/reveal.js/plugin/highlight/highlight.js'></script><script src='../slides/reveal.js/plugin/math/math.js'></script>
<script src="js/settings.js"></script>
</body>
</html>