-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-pdf.html
1437 lines (1309 loc) · 44.7 KB
/
index-pdf.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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<meta charset="utf-8">
<title>Will van Ketwich - Fibonacci Assembly Slides</title>
<meta name="description" content="Creating a Fibonacci Generator in Assembly">
<meta name="author" content="Will van Ketwich">
<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">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/default.css" id="theme">
<link rel="stylesheet" href="css/overrides.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<section>
<h1>Creating a Fibonacci Generator in Assembly</h1>
<a href="https://git.realestate.com.au/will-vanketwich/asm-talk" target="_blank">
<img src="img/trump.jpg" style="background: none; border: 0; padding-left: 135px;" />
</a>
<h3 class="em">Because ¯\_(ツ)_/¯</h3>
<p class="em2" >By Will van Ketwich</p>
<aside class="notes">
Hi, and welcome!
</aside>
</section>
<section data-background="#000000">
<h2 class="em">@wilvk</h2>
<p>
Just a dude that does stuff... mostly with computers.
</br></br>
</p>
<img src="img/me.jpeg" id="me" />
<p>
</br></br>
Systems Engineer at REA Group</br>
Group Delivery Engineering, Group Technology</br>
</p>
</section>
</section>
<section data-background="#000000">
<section>
<h2 class="em">Assembly</h2>
<p class="fragment zoom-in" data-background="#007777">
<img src="img/wat.jpg" />
</p>
<aside class="notes">
Who knows what Assembly is? Who can read Assembly? Who can write Assembly?
</aside>
</section>
<section>
<h2 class="em">Why???</h2>
<div class="fragment fade-in">
<h3>Seriously dude, why Assembly?</h3>
</br>
</div>
<div class="fragment fade-in">
<p>
<table>
<tr>
<td align="left" style="vertical-align:top;font-size:24px">
<li> The building blocks of all languages
<li> How processes work
<li> How to reverse engineer
<li> How the Linux Kernel works
<li> How device drivers work
<li> Improve troubleshooting skills
<li> Curiosity
</td>
<td style="text-align: right;">
<img src="img/one-does-not.jpg" id="meme" />
</td>
</tr>
</table>
</p>
</div>
<aside class="notes">
Once you know the fundamentals, everything else is just an implementation.
</aside>
</section>
<section>
<h2 class="em">How?</h2>
<h3>How to get started?</h3>
</br>
<table>
<tr>
<td style="vertical-align:top;font-size:36px;" width="50%">
<li> Read a few books
<li> Do some examples
<li> Define a project
<li> Look online for help
</td>
<td>
<img src="img/asm-sbs.jpg">
</td>
<td>
<img src="img/pro-asm.jpg">
</td>
</tr>
</table>
<aside class="notes">
TODO:
</aside>
</section>
</section>
<section data-background="#000000">
<section>
<h2 class="em">A Small Project</h2>
</br>
<div align="left" style="line-height: 200%;">
<ol>
<li> Reading input from the command line
<li> Get length of command line argument
<li> Converting input to a number
<li> Generating the Fibonacci number
<li> Printing the output to screen
</ol>
</br>
</br>
<pre><code style="font-size: 36px; line-height: normal;">
$ ./fib7 46
2971215073
</code></pre>
</div>
<aside class="notes">
</aside>
</section>
<section>
<h2 class="em">Decisions to be made</h2>
</br>
<div align="left" style="line-height: 200%;">
<li> Should standard libraries be used? (glibc)</br>
<li> What flavour of assembly to use?</br>NASM, GAS, MASM, YASM?</br>
<li> 64 bit or 32 bit?
</div>
<aside class="notes">
</aside>
</section>
</section>
<section data-background="#000000">
<section>
<h2 class="em">The Fibonacci Sequence</h2>
<img src="img/fib-diag.svg" height="200%" width="200%">
<aside class="notes">
</aside>
</section>
<section>
<h2 class="em">The Fibonacci Algorithm</h2>
</br>
<h3>From Wikipedia:</h3>
<div align="left" style="font-size:30px">
</br>
<p>
The sequence F<sub>n</sub> of Fibonacci numbers is defined by the recurrence relation:
</p>
<p>
</br>
F<sub>n</sub>=F<sub>n-1</sub> + F<sub>n-2</sub>
</p>
<p>
</br>
with seed values:
</p>
<p>
</br>
F<sub>1</sub>=1, F<sub>2</sub>=1
</p>
<p>
</br>
or:
</p>
<p>
</br>
F<sub>0</sub>=0, F<sub>1</sub>=1
</p>
</div>
<aside class="notes">
TODO:
</aside>
</section>
<section>
<h2 class="em">A resultant sequence</h2>
</br>
</br>
</br>
<h3>
<table>
<tr>
<td width="200px">Value</td>
<td width="30px">0</td>
<td width="30px">1</td>
<td width="30px">1</td>
<td width="30px">2</td>
<td width="30px">3</td>
<td width="30px">5</td>
<td width="30px">8</td>
<td width="60px">13</td>
<td width="60px">21</td>
<td width="60px">34</td>
<td width="60px">55</td>
<td width="60px">...</td>
</tr>
</table>
<div class="fragment fade-in">
<table>
<tr>
<td width="200px">Position</td>
<td width="30px">-</td>
<td width="30px">-</td>
<td width="30px">1</td>
<td width="30px">2</td>
<td width="30px">3</td>
<td width="30px">4</td>
<td width="30px">5</td>
<td width="60px">6</td>
<td width="60px">7</td>
<td width="60px">8</td>
<td width="60px">9</td>
<td width="60px">...</td>
</tr>
</table>
</div>
</h3>
</section>
<section>
<h2 class="em">The Fibonacci Algorithm</h2>
<h3>As C code<h3>
</br>
<pre><code style="font-size: 24px; line-height: normal;">
int fib(int n)
{
int f[n+2];
int i;
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
</code></pre>
<aside class="notes">
</aside>
</section>
</section>
<section data-background="#000000">
<section>
<h2 class="em">Beginning our implementation</h2>
<h3>And some starting knowledge</h3>
<img src="img/starting-line.jpg">
</section>
<section>
<h2 class="em">GAS Files and Language Syntax</h2>
<h3>fib1.s - doing nothing takes something</h3>
</br>
<pre><code style="font-size: 32px; line-height: normal;">
.section .text
.globl _start
_start:
nop
# the rest of our program goes here
movl $1, %eax
movl $0, %ebx
int $0x80
</code></pre>
</section>
<section>
<h2 class="em">The building blocks</h2>
<h3>Opcodes and Operands</h3>
</br>
<table style="font-size:30px;">
<thead bgcolor="#7E2F3A">
<tr>
<th>Line</th>
<th>Opcode</th>
<th>Operand 1</th>
<th>Operand 2</th>
<th>Description</th>
</tr>
</thead>
<tbody bgcolor="#322827">
<tr>
<td>nop</td>
<td>nop</td>
<td>-</td>
<td>-</td>
<td>no-operation</td>
</tr>
<tr>
<td>movl $1, %eax</td>
<td>movl</td>
<td>$1</td>
<td>%eax</td>
<td>copy 1 into register eax</td>
</tr>
<tr>
<td>movl $0, %ebx</td>
<td>movl</td>
<td>$0</td>
<td>%ebx</td>
<td>copy 0 into register ebx</td>
</tr>
<tr>
<td>int $0x80</td>
<td>int</td>
<td>$0x80</td>
<td>-</td>
<td>call interrupt number 0x80</td>
</tr>
</tbody>
</table>
<aside class="notes">
TODO:
</aside>
</section>
<section>
<h2 class="em">The tools of the trade</h2>
<h3>Assemblers and Linkers</h3>
<table>
<tr>
<td style="vertical-align:top;font-size:36px;" width="50%">
<img src="img/toolchain.png" style="display: inline-block;width: 100%;border:0px;background:rgba(255,255,255,0);">
</td>
<td style="vertical-align:middle;">
<pre><code style="font-size: 32px; line-height: normal;">
as --32 -gstabs -o fib1.o fib1.s
ld -m elf_i386 -o fib1 fib1.o
</pre></code>
</td>
</tr>
</table>
<aside class="notes">
TODO:
</aside>
</section>
<section>
<h2 class="em">The tools of the trade</h2>
<h3>Object file utilities</h3>
<div align="left">
<li> nm - lists the symbols from object files
<li> objdump - displays more information about object files
<li> elfdump - similar to objdump
<li> gdb - GNU debugger
<li> gcc - GNU compiler collection
<li> make - used for build toolchains
</div>
<aside class="notes">
TODO:
</aside>
</section>
<section>
<h2 class="em">The canvas</h2>
<h3>CPU and Registers</h3>
<img src="img/cpu-alu.png" style="width:60%;height:60%;display: inline-block;width: 80%;">
<div align="left">
</section>
<section>
<h2 class="em">The canvas</h2>
<h3>Memory Hierarchy</h3>
<img src="img/memory-hierarchy.png" style="width:60%;height:60%;display: inline-block;width: 80%;">
<div align="left">
</section>
<section>
<h2 class="em">The canvas</h2>
<h3>Registers</h3>
<img src="img/x86-registers.png" style="width:60%;height:60%;display: inline-block;width: 80%;">
<div align="left">
</section>
<section>
<h2 class="em">NOP</h2>
</br>
<div align="left" style="font-size:30px">
<li> Does nothing functional
<li> Takes up a clock cycle
<li> Can be used to fill space
</div>
</br>
<pre><code style="font-size: 32px; line-height: normal;">
nop
</code></pre>
</section>
<section>
<h2 class="em">MOV</h2>
</br>
<div align="left" style="line-height: normal;">
<li> MOVes (or copies) data from one location to another
<li> Can copy to/from memory, register and immediates
<li> Cannot copy from memory to memory
</br>
</br>
Some examples:
</br>
<pre><code style="font-size: 26px; line-height: normal;">
mov (%ebx), %eax # copy address pointed to by ebx to register eax
mov %ebx, (%eax) # copy value in ebx to address pointed to by eax
mov $1, %eax # place value 1 into register eax
mov $5, (%eax) # place value 5 into address pointed to by eax
mov %ebp, %esp # copy value in register ebp into register esp
</code></pre>
</div>
</section>
<section>
<h2 class="em">MOV - Operation Suffixes</h2>
<div align="left" style="line-height: normal;">
<li> Different variations based on size to copy
<li> Used for many instructions (MOVL, PUSHL, POPL, etc.)
<li> Floating point versions used with the FPU and SIMD
</br>
</br>
<center>
<table style="font-size:30px;">
<thead bgcolor="#7E2F3A">
<tr>
<th>Suffix</th>
<th>Type</th>
<th>Bits (Integer)</th>
<th>Bits (Floating Point)</th>
</tr>
</thead>
<tbody bgcolor="#322827">
<tr>
<td>b</td>
<td>byte</td>
<td>8</td>
<td>-</td>
</tr>
<tr>
<td>s</td>
<td>short</td>
<td>16</td>
<td>32</td>
</tr>
<tr>
<td>w</td>
<td>word</td>
<td>16</td>
<td>-</td>
</tr>
<tr>
<td>l</td>
<td>long</td>
<td>32</td>
<td>64</td>
</tr>
<tr>
<td>q</td>
<td>quad</td>
<td>64</td>
<td>-</td>
</tr>
<tr>
<td>t</td>
<td>ten bytes</td>
<td>-</td>
<td>80</td>
</tr>
</tbody>
</table>
</center>
</br>
Some examples:
<pre><code style="font-size: 26px; line-height: normal;">
movb $2, (%ebx) # moves 0x02 into ebx
movw $2, (%ebx) # moves 0x0002 into ebx
movl $2, (%ebx) # moves 0x00000002 into ebx
</code></pre>
</div>
</section>
<section>
<h2 class="em">INT</h2>
<div align="left" style="line-height: normal;">
<li> Interrupts the CPU from scheduled processing
<li> Most interrupts are hardware-based
<li> Software-based interrupts are called 'soft-interrupts'
<li> Requesting actions from User Space in Kernel Space
<ul style="margin-left: 100px;">
<li> Linux uses INT 0x80
<li> MacOS uses INT 0x60
<li> Windows uses INT 0x21
</ul>
<pre><code style="font-size: 32px; line-height: normal;">
# exit with return code 0
movl $1, %eax
movl $0, %ebx
int 0x80
</code></pre>
<li> Modern CPUs/OSes use SYSCALL but INT is still valid
</div>
</section>
</section>
<section data-background="#000000">
<section>
<h2 class="em">1. Reading input from the</br>command line</h2>
</br>
<ol>
<li> <font color="red">Reading input from the command line</font>
<li> Get length of command line argument
<li> Converting input to a number
<li> Generating the Fibonacci number
<li> Printing the output to screen
</ol>
</section>
<section>
<h2 class="em">Memory Layout</h2>
<img src="img/mem-layout.jpg" style="display: inline-block;width: 80%;">
</section>
<section>
<h2 class="em">Command Line Arguments in the Stack</h2>
</br>
<table style="font-size: 32px; line-height: normal;">
<thead bgcolor="#7E2F3A">
<tr>
<th style="width:200px;">Stack</th>
<th>Data</th>
</tr>
</thead>
<tbody bgcolor="#322827">
<tr>
<td>...</td>
<td></td>
</tr>
<tr>
<td>ESP + n+8</td>
<td>< pointer to second environment variable ></td>
</tr>
<tr>
<td>ESP + n+4</td>
<td>< pointer to first environment variable ></td>
</tr>
<tr>
<td>ESP + n</td>
<td>< NULL for end of command line arguments ></td>
</tr>
<tr>
<td>ESP + n-4</td>
<td>< pointer to last string argument ></td>
</tr>
<tr>
<td>...</td>
<td></td>
</tr>
<tr>
<td>ESP + 12</td>
<td>< pointer to second string argument ></td>
</tr>
<tr>
<td>ESP + 8</td>
<td>< pointer to a string that contains the first argument ></td>
</tr>
<tr>
<td>ESP + 4</td>
<td>< pointer to a string containing the name of the application ></td>
</tr>
<tr>
<td>ESP + 0</td>
<td>< number of arguments on command line ></td>
</tr>
<tr>
<td>...</td>
<td></td>
</tr>
</tbody>
</table>
</section>
<section>
<h2 class="em">A Simple Print Utility</h2>
<h3>fib2.s</h3>
</br>
<pre style="height: 500px;"><code style="font-size: 22px; line-height: normal; max-height:500px;height:500px;">
# stack args example
.section .text
.globl _start
_start:
nop
movl %esp, %ebp # take a copy of the stack pointer esp into ebp
addl $8, %ebp # address of first arg in stack
movl (%ebp), %ecx # move the address of the first arg into ecx
movl $4, %edx # set the length of our string to 4
movl $4, %eax # indicate to int 0x80 that we are doing a write
movl $0, %ebx # indicate to int 0x80 that we are writing to file descriptor 0
int $0x80 # call int 0x80 for write
movl $1, %eax # exit gracefully
movl $0, %ebx # with return code 0
int $0x80 # call int 0x80 for exit
</code></pre>
</section>
<section>
<h2 class="em">Viewing the Stack in GDB</h2>
<h3> A demo</h3>
</br>
<img src="img/demo-gods.jpg">
<aside class="notes">
gas-asm-fib
./docker-shell
gdb --args ./fib2 test
list
b 5
r
info registers
stepi up to line 10
x/5s ($ecx)
c
q
</aside>
</section>
</section>
<section data-background="#000000">
<section>
<h2 class="em">2. Get length of command line argument</h2>
</br>
<ol>
<li> Reading input from the command line
<li> <font color="red">Get length of command line argument</font>
<li> Converting input to a number
<li> Generating the Fibonacci number
<li> Printing the output to screen
</ol>
</section>
<section>
<h2 class="em">Approach</h2>
</br>
<div align="left">
<li> Find address of first command line argument string
<li> Get length of string
<li> Print string to stdout
</div>
</section>
<section style="height: 900px">
<h2 class="em">Source</h2>
<h3>fib3.s</h3>
<pre style="height: 600px;"><code style="font-size: 18px; line-height: normal; max-height:600px;height:600px;">
# framework - get first argument from the command line and print to stdout
.section .text
.globl _start
_start:
nop
movl %esp, %ebp # take a copy of esp to use
addl $8, %ebp # address of first arg in stack
movl (%ebp), %edi # move arg address into esi for scasb
push %edi # store the string address as edi gets clobbered
movl $50, %ecx # set ecx counter to a high value
movl $0, %eax # zero al search char
movl %ecx, %ebx # copy our max counter value to edx
cld # set direction down
repne scasb # iterate until we find the al char
subl %ecx, %ebx # subtract from our original ecx value
dec %ebx # remove null byte at the end of the string from the count
pop %ecx # restore our string address into ecx
mov %ebx, %edx # move our count value to edx for the int 80 call
movl $4, %eax # set eax to 4 for int 80 to write to file
movl $0, %ebx # set ebx for file to write to as stdoout (file descriptor 0)
int $0x80 # make it so
movl $1, %eax # set eax for int 80 for system exit
movl $0, %ebx # set ebx for return code 0
int $0x80 # make it so again
</code></pre>
</section>
<section>
<h2 class="em">Source</h2>
<h3>New Code</h3>
</br>
<pre><code style="font-size: 22px; line-height: normal;">
movl (%ebp), %edi # move arg address into esi for scasb
push %edi # store the string address as edi gets clobbered
movl $50, %ecx # set ecx counter to a high value
movl $0, %eax # zero al search char
movl %ecx, %ebx # copy our max counter value to edx
cld # set direction down
repne scasb # iterate until we find the al char
subl %ecx, %ebx # subtract from our original ecx value
dec %ebx # remove null byte at the end of the string from the count
pop %ecx # restore our string address into ecx
mov %ebx, %edx # move our count value to edx for the int 80 call
</code></pre>
</section>
<section>
<h2 class="em">repne scasb</h2>
<h3>Scanning Strings</h3>
<div align="left" style="line-height: normal;">
</br>
<li> edi - points to address of string to scan
<li> al - stores the byte to scan until
<li> ecx - stores the result of the scan count
</br>
</br>
<li> requires registers set up before running (like INT)
</br>
</br>
<li> the cld opcode clears the direction flag
<li> each iteration increases edi and decreases ecx
</div>
</section>
<section>
<h2 class="em">Some Arithmetic</h2>
<div align="left" style="line-height: normal;">
</br>
<table>
<tr>
<td>
ecx = 50 - ( len(string) + 1 )
</td>
</tr>
<tr>
<td>
</br>
but we want len(string)
</br>
</td>
</tr>
<tr>
<td>
</br>
len(string) + 1 = 50 - ecx
</br>
</td>
</tr>
<tr>
<td>
len(string) = (50 - ecx) - 1
</br>
</td>
</tr>
</table>
</div>
</br>
<pre><code style="font-size: 24px; line-height: normal;">
subl %ecx, %ebx # subtract from our original ecx value
dec %ebx # remove null byte at the end of the string from the count
</code></pre>
</section>
<section>
<h2 class="em">Labels, CALL, RET and the Stack</h2>
<h3>labels</h3>
</br>
<div align="left" style="line-height: normal;">
<li> points to an addresses in memory
<li> label addresses resolved with ld
</br>
</br>
</br>
Standard labels:
<pre style="font-size:30px;"><code>my_function:</pre></code>
</br>
</br>
Local labels:
<pre style="font-size:30px;"><code>.my_local_function:</pre></code>
</div>
</section>
<section>
<h2 class="em">Labels, CALL, RET and the Stack</h2>
<h3> CALL and RET</h3>
</br>
<div align="left" style="line-height: normal;">
CALL:
</br>
<li>places next instruction address on the stack
<li>jumps execution to address of label
</br>
</br>
RET:
</br>
<li> RET jumps execution back to the next address on the stack
<li> functions are just a logical placing of CALL and RET
<li> no set order to location of labels
</div>
</section>
<section>
<h2 class="em">Functions</h2>
<h3> A Simple Example </h3>
</br>
<div align="left" style="line-height:normal;">
<pre><code style="font-size: 30px; line-height: normal;">
call get_string_length
mov %eax, %ebx
...
get_string_length:
mov $1, %eax
ret
</code></pre>
</section>
<section>
<h2 class="em">The implementation</h2>
<h3>fib4.s</h3>
<pre style="height: 900px;"><code style="font-size: 13px; line-height: normal; max-height:700px;height:700px;">
# framework - refactor into separate functions
.section .text
.globl _start
# entrypoint of application
_start:
nop
movl %esp, %ebp # take a copy of esp to use
addl $8, %ebp # address of first arg in stack
movl (%ebp), %edi # move arg address into esi for scasb
push %edi # store the string address as edi gets clobbered
call get_string_length
pop %ecx # restore our string address into ecx
call print_string
call exit
# get length of string pointed to by edi and place result in ebx
get_string_length:
movl $50, %ecx # set ecx counter to a high value
movl $0, %eax # zero al search char
movl %ecx, %ebx # copy our max counter value to edx
cld # set direction down
repne scasb # iterate until we find the al char
movl %ecx, %edx # move count into edx
subl %ecx, %ebx # subtract from our original ecx value
dec %ebx # remove null byte at the end of the string from the count
ret
# print the string in ecx to the length of ebx
print_string:
mov %ebx, %edx # move our count value to edx for the int 80 call
movl $4, %eax # set eax to 4 for int 80 to write to file
movl $0, %ebx # set ebx for file to write to as stdoout (file descriptor 0)
int $0x80 # make it so
ret
# exit the application
exit:
movl $1, %eax # set eax for int 80 for system exit
movl $0, %ebx # set ebx for return code 0
int $0x80 # make it so again
</code></pre>
</section>
</section>
<section data-background="#000000">
<section>
<h2 class="em">3. Converting input to a number</h2>
</br>
<ol>
<li> Reading input from the command line
<li> Get length of command line argument
<li> <font color="red">Converting input to a number</font>
<li> Generating the Fibonacci number
<li> Printing the output to screen
</ol>
</section>
<section>
<h2 class="em">long_from_string</h2>
<h3>As (pseudo) C code</h3>
</br>
<pre style="height: 500px;"><code style="font-size: 24px; line-height: normal; max-height:500px;height:500px;">
long long_from_string(*char number_string)
{
int i=0;
long return_value=0;
int temp_value=0;
char digit = number_string[i];
while(digit >= ‘0’ && digit <= ‘9’)
{
temp_value = digit - 48;
return_value *= 10;
return_value += temp_value;
digit = number_string[++i];
}
return return_value;
}
</code></pre>
</section>
<section>
<h2 class="em">long_from_string</h2>
<h3>fib5.s</h3>
<pre style="height: 500px;"><code style="font-size: 24px; line-height: normal; max-height:500px;height:500px;">
long_from_string:
xor %eax, %eax # set eax as our result register
xor %ecx, %ecx # set ecx(cl) as our temporary byte register
.top:
movb (%edi), %cl
inc %edi
cmpb $48, %cl # check if value in ecx is less than ascii '0'. exit if less
jl .done
cmpb $57, %cl # check if value in ecx is greater than ascii '9'. exit if greater
jg .done
sub $48, %cl
imul $10, %eax
add %ecx, %eax
jmp .top
.done:
ret
</code></pre>
</section>
<section>
<h2 class="em">ASCII</h2>
<img src="img/ascii-table.gif">
</section>
<section>
<h2 class="em">Compare and Jump Opcodes</h2>
</br>
<div align="left" style="line-height:normal;">
<li> CMP compares two values and sets flags based on the result
<li> Flags include carry, zero, overflow/underflow
<li> The flags are used to jump to a label/address
</br>
</br> Many types of jump flags:
<li> jg = jump if greater than
<li> jl = jump if less than
<li> je = jump if equal