stdio-c.asm
36 KB
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
# Generated by cct
# Franck Pommereau (2018)
# Adapted from Atul Varma's c.py (Spring 2004)
#
# on computer start: call main and halt
#
IRQ0:
set R9 @main
call R9
halt
#
# code from file 'stdio.c'
#
# BEGIN FUNCTION: puts() -----------------------------------------------
#
# Function type: function(pointer(char))->int
# stdio.c:32 (enter function)
puts:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 2 # Allocate 2 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
push R5 # Save callee-save register
push R4 # Save callee-save register
# stdio.c:33
# stdio.c:35
set R9 -2 # Get BP-relative address of n
add R9 BP R7 # Compute address of n
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R7 # ... save value
# stdio.c:36
set R9 -1 # Get BP-relative address of i
add R9 BP R6 # Compute address of i
set R8 0 # Use constant 0
mov R8 R7 # Assignement '=': set result
st R7 R6 # ... save value
# stdio.c:37
# WHILE loop - begin
_L1_test:
set R7 1 # Load numeric constant 1
set R9 @_L2_done # Point towards loop exit
jz R7 R9 # ... if result is zero, jump to it
# stdio.c:38
# IF statment - begin
ldi R7 3 # Get value of s
ldi R6 -1 # Get value of i
add R7 R6 R5 # Load addr of array index
ld R5 R5 # Load array value
set R9 @_L4_else # Point towards else clause
jz R5 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:39
set R9 -2 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R7 3 # Get value of s
ldi R6 -1 # Get value of i
add R7 R6 R4 # Load addr of array index
ld R4 R4 # Load array value
push R4 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# stdio.c:40
set R9 -1 # Get BP-relative address of i
add R9 BP R4 # Compute address of i
set R8 1 # Use constant 1
ld R4 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R4 # ... save value
# IF statment - THEN clause - end
set R9 @_L3_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L4_else:
# stdio.c:41
# <string>:0
set R9 @_L2_done # Loop: break statement
jmp R9 # ... jump to loop exit
# IF statment - ELSE clause - end
_L3_done:
# IF statment - end
set R9 @_L1_test # Point towards loop start
jmp R9 # ... jump to it
_L2_done:
# WHILE loop - end
# stdio.c:45
ldi R5 -2 # Get value of n
mov R5 R0 # Set return value
set R9 @_L0_function_end # Point towards function exit
jmp R9 # ... jump to it
# stdio.c:32 (exit function)
_L0_function_end:
pop R4 # Restore callee-save register
pop R5 # Restore callee-save register
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: puts() -------------------------------------------------
# BEGIN FUNCTION: putu() -----------------------------------------------
#
# Function type: function(int,char)->int
# stdio.c:55 (enter function)
putu:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 6 # Allocate 6 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
push R5 # Save callee-save register
push R4 # Save callee-save register
# stdio.c:56
# stdio.c:62
set R9 -4 # Get BP-relative address of DIGITS
add R9 BP R7 # Compute address of DIGITS
set R6 @_LC0 # Get addr of string literal '0123456...'
mov R6 R5 # Assignement '=': set result
st R5 R7 # ... save value
# stdio.c:63
set R9 -1 # Get BP-relative address of str
add R9 BP R5 # Compute address of str
set R7 @_LC1 # Get addr of string literal '65536'
mov R7 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:64
set R9 -3 # Get BP-relative address of n
add R9 BP R6 # Compute address of n
set R8 0 # Use constant 0
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# stdio.c:65
set R9 -2 # Get BP-relative address of p
add R9 BP R5 # Compute address of p
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:66
# IF statment - begin
ldi R6 4 # Get value of f
set R8 78 # Use constant 120
eq R6 R8 R6 # Perform '=='
set R9 @_L7_else # Point towards else clause
jz R6 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:67
set R9 -5 # Get BP-relative address of div
add R9 BP R6 # Compute address of div
set R8 10 # Use constant 16
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# IF statment - THEN clause - end
set R9 @_L6_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L7_else:
# stdio.c:69
set R9 -5 # Get BP-relative address of div
add R9 BP R5 # Compute address of div
set R8 A # Use constant 10
mov R8 R6 # Assignement '=': set result
st R6 R5 # ... save value
# IF statment - ELSE clause - end
_L6_done:
# IF statment - end
# stdio.c:71
# WHILE loop - begin
_L8_test:
ldi R6 3 # Get value of u
set R9 @_L9_done # Point towards loop exit
jz R6 R9 # ... if result is zero, jump to it
# stdio.c:72
set R9 -6 # Get BP-relative address of r
add R9 BP R6 # Compute address of r
ldi R5 3 # Get value of u
ldi R7 -5 # Get value of div
mod R5 R7 R5 # Perform '%'
mov R5 R7 # Assignement '=': set result
st R7 R6 # ... save value
# stdio.c:73
ldi R7 -1 # Get value of str
ldi R6 -2 # Get value of p
add R7 R6 R5 # Load addr of array index
ldi R7 -4 # Get value of DIGITS
ldi R6 -6 # Get value of r
add R7 R6 R4 # Load addr of array index
ld R4 R4 # Load array value
mov R4 R7 # Assignement '=': set result
st R7 R5 # ... save value
# stdio.c:74
set R9 -2 # Get BP-relative address of p
add R9 BP R7 # Compute address of p
set R8 1 # Use constant 1
ld R7 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R7 # ... save value
# stdio.c:75
set R9 3 # Get BP-relative address of u
add R9 BP R5 # Compute address of u
ldi R7 3 # Get value of u
ldi R4 -5 # Get value of div
div R7 R4 R7 # Perform '/'
mov R7 R4 # Assignement '=': set result
st R4 R5 # ... save value
set R9 @_L8_test # Point towards loop start
jmp R9 # ... jump to it
_L9_done:
# WHILE loop - end
# stdio.c:77
set R9 -2 # Get BP-relative address of p
add R9 BP R4 # Compute address of p
set R8 1 # Use constant 1
ld R4 R5 # Assignement '-=': load initial left value
sub R5 R8 R5 # ... subtract right value
st R5 R4 # ... save value
# stdio.c:78
# WHILE loop - begin
_L10_test:
ldi R5 -2 # Get value of p
set R8 0 # Use constant 0
inc R5 # Perform 'a >= b'
gt R5 R8 R5 # ... that is 'a+1 > b'
set R9 @_L11_done # Point towards loop exit
jz R5 R9 # ... if result is zero, jump to it
# stdio.c:79
set R9 -3 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R4 -1 # Get value of str
ldi R7 -2 # Get value of p
add R4 R7 R6 # Load addr of array index
ld R6 R6 # Load array value
push R6 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R6 # Assignement '+=': load initial left value
add R6 R0 R6 # ... add right value
st R6 R5 # ... save value
# stdio.c:80
set R9 -2 # Get BP-relative address of p
add R9 BP R6 # Compute address of p
set R8 1 # Use constant 1
ld R6 R5 # Assignement '-=': load initial left value
sub R5 R8 R5 # ... subtract right value
st R5 R6 # ... save value
set R9 @_L10_test # Point towards loop start
jmp R9 # ... jump to it
_L11_done:
# WHILE loop - end
# stdio.c:82
ldi R5 -3 # Get value of n
mov R5 R0 # Set return value
set R9 @_L5_function_end # Point towards function exit
jmp R9 # ... jump to it
# stdio.c:55 (exit function)
_L5_function_end:
pop R4 # Restore callee-save register
pop R5 # Restore callee-save register
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: putu() -------------------------------------------------
# BEGIN FUNCTION: puti() -----------------------------------------------
#
# Function type: function(int)->int
# stdio.c:85 (enter function)
puti:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 1 # Allocate 1 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
# stdio.c:86
# stdio.c:87
set R9 -1 # Get BP-relative address of n
add R9 BP R7 # Compute address of n
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R7 # ... save value
# stdio.c:88
# IF statment - begin
ldi R6 3 # Get value of i
set R8 7FFF # Use constant 32767
gt R6 R8 R6 # Perform '>'
set R9 @_L14_else # Point towards else clause
jz R6 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:89
set R9 -1 # Get BP-relative address of n
add R9 BP R6 # Compute address of n
# FUNCTION CALL to putc() - begin
set R8 2D # Use constant 45
push R8 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R6 R7 # Assignement '+=': load initial left value
add R7 R0 R7 # ... add right value
st R7 R6 # ... save value
# stdio.c:90
set R9 -1 # Get BP-relative address of n
add R9 BP R7 # Compute address of n
# FUNCTION CALL to putu() - begin
set R8 75 # Use constant 117
push R8 # Push arg 2
ldi R6 3 # Get value of i
neg R6 R6 # Perform unary negation
push R6 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
ld R7 R6 # Assignement '+=': load initial left value
add R6 R0 R6 # ... add right value
st R6 R7 # ... save value
# stdio.c:91
ldi R6 -1 # Get value of n
mov R6 R0 # Set return value
set R9 @_L12_function_end # Point towards function exit
jmp R9 # ... jump to it
# IF statment - THEN clause - end
set R9 @_L13_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L14_else:
# stdio.c:93
# FUNCTION CALL to putu() - begin
set R8 75 # Use constant 117
push R8 # Push arg 2
ldi R6 3 # Get value of i
push R6 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
mov R0 R0 # Set return value
set R9 @_L12_function_end # Point towards function exit
jmp R9 # ... jump to it
# IF statment - ELSE clause - end
_L13_done:
# IF statment - end
# stdio.c:85 (exit function)
_L12_function_end:
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: puti() -------------------------------------------------
# BEGIN FUNCTION: printf() ---------------------------------------------
#
# Function type: function(pointer(char))->int
# stdio.c:97 (enter function)
printf:
push BP # Save old frame pointer
mov SP BP # Set new frame pointer
set R9 9 # Allocate 9 words for local+temp vars
sub SP R9 SP # ... shift SP
push R7 # Save callee-save register
push R6 # Save callee-save register
push R5 # Save callee-save register
push R4 # Save callee-save register
# stdio.c:98
# stdio.c:103
set R9 -1 # Get BP-relative address of iargs
add R9 BP R7 # Compute address of iargs
set R9 3 # Get BP-relative address of str
add R9 BP R6 # Compute address of str
# (Address-of operator '&' used here)
mov R6 R5 # Assignement '=': set result
st R5 R7 # ... save value
# stdio.c:104
set R9 -3 # Get BP-relative address of cargs
add R9 BP R5 # Compute address of cargs
set R9 3 # Get BP-relative address of str
add R9 BP R7 # Compute address of str
# (Address-of operator '&' used here)
mov R7 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:105
set R9 -5 # Get BP-relative address of a
add R9 BP R6 # Compute address of a
set R8 1 # Use constant 1
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# stdio.c:106
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
set R8 0 # Use constant 0
mov R8 R6 # Assignement '=': set result
st R6 R5 # ... save value
# stdio.c:107
set R9 -2 # Get BP-relative address of i
add R9 BP R6 # Compute address of i
set R8 0 # Use constant 0
mov R8 R5 # Assignement '=': set result
st R5 R6 # ... save value
# stdio.c:108
# WHILE loop - begin
_L16_test:
set R5 1 # Load numeric constant 1
set R9 @_L17_done # Point towards loop exit
jz R5 R9 # ... if result is zero, jump to it
# stdio.c:109
# IF statment - begin
ldi R5 3 # Get value of str
ldi R6 -2 # Get value of i
add R5 R6 R7 # Load addr of array index
ld R7 R7 # Load array value
set R8 0 # Use constant 0
eq R7 R8 R7 # Perform '=='
set R9 @_L19_else # Point towards else clause
jz R7 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# <string>:0
set R9 @_L17_done # Loop: break statement
jmp R9 # ... jump to loop exit
# IF statment - THEN clause - end
set R9 @_L18_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L19_else:
# stdio.c:111
# IF statment - begin
ldi R7 3 # Get value of str
ldi R5 -2 # Get value of i
add R7 R5 R6 # Load addr of array index
ld R6 R6 # Load array value
set R8 25 # Use constant 37
eq R6 R8 R6 # Perform '=='
set R9 @_L21_else # Point towards else clause
jz R6 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:112
set R9 -2 # Get BP-relative address of i
add R9 BP R6 # Compute address of i
set R8 1 # Use constant 1
ld R6 R7 # Assignement '+=': load initial left value
add R7 R8 R7 # ... add right value
st R7 R6 # ... save value
# stdio.c:113
# IF statment - begin
ldi R7 3 # Get value of str
ldi R6 -2 # Get value of i
add R7 R6 R5 # Load addr of array index
ld R5 R5 # Load array value
set R8 69 # Use constant 105
eq R5 R8 R5 # Perform '=='
set R9 @_L23_else # Point towards else clause
jz R5 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:114
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to puti() - begin
ldi R7 -1 # Get value of iargs
ldi R6 -5 # Get value of a
add R7 R6 R4 # Load addr of array index
ld R4 R4 # Load array value
push R4 # Push arg 1
set R9 @puti # Point towards function puti()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to puti() - end
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L22_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L23_else:
# stdio.c:115
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 75 # Use constant 117
eq R0 R8 R0 # Perform '=='
set R9 @_L25_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:116
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to putu() - begin
sti R0 -6 # Save caller-save register to temp
set R8 75 # Use constant 117
push R8 # Push arg 2
ldi R0 -1 # Get value of iargs
ldi R4 -5 # Get value of a
add R0 R4 R5 # Load addr of array index
ld R5 R5 # Load array value
push R5 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
ldi R5 -6 # Stack machine: copy temp to register
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L24_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L25_else:
# stdio.c:117
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 73 # Use constant 115
eq R0 R8 R0 # Perform '=='
set R9 @_L27_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:118
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to puts() - begin
sti R0 -7 # Save caller-save register to temp
ldi R0 -3 # Get value of cargs
ldi R4 -5 # Get value of a
add R0 R4 R5 # Load addr of array index
ld R5 R5 # Load array value
push R5 # Push arg 1
set R9 @puts # Point towards function puts()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to puts() - end
ldi R5 -7 # Stack machine: copy temp to register
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L26_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L27_else:
# stdio.c:119
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 78 # Use constant 120
eq R0 R8 R0 # Perform '=='
set R9 @_L29_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:120
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to putu() - begin
sti R0 -8 # Save caller-save register to temp
set R8 78 # Use constant 120
push R8 # Push arg 2
ldi R0 -1 # Get value of iargs
ldi R4 -5 # Get value of a
add R0 R4 R5 # Load addr of array index
ld R5 R5 # Load array value
push R5 # Push arg 1
set R9 @putu # Point towards function putu()
call R9 # ... call it
set R9 2 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putu() - end
ldi R5 -8 # Stack machine: copy temp to register
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - THEN clause - end
set R9 @_L28_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L29_else:
# stdio.c:121
# IF statment - begin
ldi R4 3 # Get value of str
ldi R5 -2 # Get value of i
add R4 R5 R0 # Load addr of array index
ld R0 R0 # Load array value
set R8 25 # Use constant 37
eq R0 R8 R0 # Perform '=='
set R9 @_L31_else # Point towards else clause
jz R0 R9 # ... if result is zero, jump to it
# IF statment - THEN clause - begin
# stdio.c:122
set R9 -4 # Get BP-relative address of n
add R9 BP R0 # Compute address of n
# FUNCTION CALL to putc() - begin
sti R0 -9 # Save caller-save register to temp
set R8 25 # Use constant 37
push R8 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ldi R4 -9 # Stack machine: copy temp to register
ld R4 R5 # Assignement '+=': load initial left value
add R5 R0 R5 # ... add right value
st R5 R4 # ... save value
# stdio.c:123
set R9 -2 # Get BP-relative address of i
add R9 BP R5 # Compute address of i
set R8 1 # Use constant 1
ld R5 R4 # Assignement '+=': load initial left value
add R4 R8 R4 # ... add right value
st R4 R5 # ... save value
# <string>:0
set R9 @_L16_test # Loop: continue statement
jmp R9 # ... jump to loop start
# IF statment - THEN clause - end
set R9 @_L30_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L31_else:
# stdio.c:126
set R9 -4 # Get BP-relative address of n
add R9 BP R4 # Compute address of n
# FUNCTION CALL to putc() - begin
set R8 25 # Use constant 37
push R8 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R4 R5 # Assignement '+=': load initial left value
add R5 R0 R5 # ... add right value
st R5 R4 # ... save value
# stdio.c:127
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R4 3 # Get value of str
ldi R0 -2 # Get value of i
add R4 R0 R7 # Load addr of array index
ld R7 R7 # Load array value
push R7 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R7 # Assignement '+=': load initial left value
add R7 R0 R7 # ... add right value
st R7 R5 # ... save value
# IF statment - ELSE clause - end
_L30_done:
# IF statment - end
# IF statment - ELSE clause - end
_L28_done:
# IF statment - end
# IF statment - ELSE clause - end
_L26_done:
# IF statment - end
# IF statment - ELSE clause - end
_L24_done:
# IF statment - end
# IF statment - ELSE clause - end
_L22_done:
# IF statment - end
# stdio.c:129
set R9 -5 # Get BP-relative address of a
add R9 BP R7 # Compute address of a
set R8 1 # Use constant 1
ld R7 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R7 # ... save value
# IF statment - THEN clause - end
set R9 @_L20_done # Point towards if end
jmp R9 # ... jump to it
# IF statment - ELSE clause - begin
_L21_else:
# stdio.c:131
set R9 -4 # Get BP-relative address of n
add R9 BP R5 # Compute address of n
# FUNCTION CALL to putc() - begin
ldi R7 3 # Get value of str
ldi R0 -2 # Get value of i
add R7 R0 R4 # Load addr of array index
ld R4 R4 # Load array value
push R4 # Push arg 1
set R9 @putc # Point towards function putc()
call R9 # ... call it
set R9 1 # Deallocate argument stack
add R9 SP SP # ... shift SP
# FUNCTION CALL to putc() - end
ld R5 R4 # Assignement '+=': load initial left value
add R4 R0 R4 # ... add right value
st R4 R5 # ... save value
# IF statment - ELSE clause - end
_L20_done:
# IF statment - end
# IF statment - ELSE clause - end
_L18_done:
# IF statment - end
# stdio.c:133
set R9 -2 # Get BP-relative address of i
add R9 BP R4 # Compute address of i
set R8 1 # Use constant 1
ld R4 R5 # Assignement '+=': load initial left value
add R5 R8 R5 # ... add right value
st R5 R4 # ... save value
set R9 @_L16_test # Point towards loop start
jmp R9 # ... jump to it
_L17_done:
# WHILE loop - end
# stdio.c:135
ldi R5 -4 # Get value of n
mov R5 R0 # Set return value
set R9 @_L15_function_end # Point towards function exit
jmp R9 # ... jump to it
# stdio.c:97 (exit function)
_L15_function_end:
pop R4 # Restore callee-save register
pop R5 # Restore callee-save register
pop R6 # Restore callee-save register
pop R7 # Restore callee-save register
mov BP SP # Deallocate local+temp vars
pop BP # Restore old stack frame
ret
# END FUNCTION: printf() -----------------------------------------------
#
# string literals from file 'stdio.c'
#
_LC0:
str "0123456789ABCDEF\0"
_LC1:
str "65536\0"