Commit 61d5169

mo khan <mo@mokhan.ca>
2025-01-18 20:59:06
Embed the C code and Assembly code in the generated pdf
1 parent 9d11785
assignments/3/main.c
@@ -1,20 +1,16 @@
+#include <limits.h>
 #include <stdio.h>
 
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
   int number = 0;
-  int max = -1;
-  int min = -1;
+  int max = INT_MIN;
+  int min = INT_MAX;
 
-  while(scanf("%d", &number) != EOF) {
-    if (max == -1 || number > max) {
+  while (scanf("%d", &number) != EOF) {
+    if (number > max)
       max = number;
-    }
-    if (min == -1 || number < min) {
+    if (number < min)
       min = number;
-    }
-
-    number = 0;
   }
 
   printf("Max: %d\nMin: %d\n", max, min);
assignments/3/main.exe
Binary file
assignments/3/main.s
@@ -22,60 +22,49 @@ main:
 	pushq	%rbp	#
 	.cfi_def_cfa_offset 24
 	.cfi_offset 6, -24
-# main.c:7:   int min = -1;
-	movl	$-1, %ebp	#, min
-# main.c:4: {
+# main.c:7:   int min = INT_MAX;
+	movl	$2147483647, %ebp	#, min
+# main.c:4: int main(int argc, char *argv[]) {
 	pushq	%rbx	#
 	.cfi_def_cfa_offset 32
 	.cfi_offset 3, -32
-# main.c:6:   int max = -1;
-	movl	$-1, %ebx	#, max
-# main.c:4: {
+# main.c:6:   int max = INT_MIN;
+	movl	$-2147483648, %ebx	#, max
+# main.c:4: int main(int argc, char *argv[]) {
 	subq	$16, %rsp	#,
 	.cfi_def_cfa_offset 48
+# main.c:5:   int number = 0;
+	movl	$0, 12(%rsp)	#, number
 	leaq	12(%rsp), %r12	#, tmp107
-# main.c:9:   while(scanf("%d", &number) != EOF) {
+# main.c:9:   while (scanf("%d", &number) != EOF) {
 	jmp	.L2	#
 	.p2align 4,,10
 	.p2align 3
-.L5:
-# main.c:10:     if (max == -1 || number > max) {
-	movl	12(%rsp), %eax	# number, pretmp_20
-# main.c:10:     if (max == -1 || number > max) {
-	cmpl	%eax, %ebx	# pretmp_20, max
-	movl	%eax, %edx	# pretmp_20, tmp108
-	cmovge	%ebx, %edx	# max,, tmp108
-	cmpl	$-1, %ebx	#, max
-	movl	%edx, %ebx	# tmp108, tmp108
-# main.c:13:     if (min == -1 || number < min) {
-	movl	%eax, %edx	# pretmp_20, tmp110
-# main.c:10:     if (max == -1 || number > max) {
-	cmove	%eax, %ebx	# tmp108,, pretmp_20, tmp108
-# main.c:13:     if (min == -1 || number < min) {
-	cmpl	%eax, %ebp	# pretmp_20, min
-	cmovle	%ebp, %edx	# min,, tmp110
-	cmpl	$-1, %ebp	#, min
-	cmovne	%edx, %eax	# tmp110,, pretmp_20
-	movl	%eax, %ebp	# pretmp_20, min
+.L3:
+# main.c:10:     if (number > max)
+	movl	12(%rsp), %eax	# number, number.0_1
+# main.c:10:     if (number > max)
+	cmpl	%eax, %ebx	# number.0_1, max
+	cmovl	%eax, %ebx	# max,, number.0_1, max
+# main.c:12:     if (number < min)
+	cmpl	%eax, %ebp	# number.0_1, min
+	cmovg	%eax, %ebp	# min,, number.0_1, min
 .L2:
-# main.c:9:   while(scanf("%d", &number) != EOF) {
+# main.c:9:   while (scanf("%d", &number) != EOF) {
 	xorl	%eax, %eax	#
 	movq	%r12, %rsi	# tmp107,
 	movl	$.LC0, %edi	#,
-# main.c:5:   int number = 0;
-	movl	$0, 12(%rsp)	#, number
-# main.c:9:   while(scanf("%d", &number) != EOF) {
 	call	__isoc99_scanf	#
-# main.c:9:   while(scanf("%d", &number) != EOF) {
-	cmpl	$-1, %eax	#, tmp112
-	jne	.L5	#,
-# main.c:20:   printf("Max: %d\nMin: %d\n", max, min);
+# main.c:9:   while (scanf("%d", &number) != EOF) {
+	cmpl	$-1, %eax	#, tmp108
+	jne	.L3	#,
+# main.c:16:   printf("Max: %d\nMin: %d\n", max, min);
 	movl	%ebp, %edx	# min,
 	movl	%ebx, %esi	# max,
 	movl	$.LC1, %edi	#,
 	xorl	%eax, %eax	#
 	call	printf	#
-# main.c:21: }
+# main.c:17: }
 	addq	$16, %rsp	#,
 	.cfi_def_cfa_offset 32
 	xorl	%eax, %eax	#
assignments/3/Makefile
@@ -7,7 +7,7 @@ main.exe: main.c
 	gcc -o main.exe main.c
 
 numbers.txt:
-	ruby -e '100.times { IO.write("numbers.txt", "#{rand(100_000_000)}\n", mode: "a+") }'
+	ruby -e '100.times { IO.write("numbers.txt", "#{rand(100_000)}\n", mode: "a+") }'
 
 run: main.exe numbers.txt
 	./main.exe < numbers.txt
assignments/3/numbers.txt
@@ -1,100 +1,100 @@
-92466815
-74269497
-87050963
-25751600
-8977277
-77496058
-30567041
-5491932
-73316075
-62877755
-45131658
-23209575
-52124207
-68541577
-96103422
-62574138
-46543308
-69316651
-80018420
-75836516
-58878100
-6148427
-44641940
-9142229
-61646141
-14736493
-17563644
-61133076
-48271908
-54418408
-84568705
-15645588
-57940938
-10026192
-8379555
-85693099
-8887701
-51277614
-86893573
-29301811
-9482011
-16525518
-55418577
-85246213
-99702148
-43592251
-89572153
-47687764
-16903422
-54985874
-21405561
-38938206
-40252034
-77770613
-84641140
-15944022
-20266861
-24590849
-73021859
-32635053
-65623867
-46364251
-89109658
-13712279
-86114503
-5544717
-63892
-66769336
-41908253
-92439154
-39785844
-39528483
-77990612
-4983638
-47246349
-32947081
-94376839
-1540652
-2165773
-60160647
-16783606
-35466343
-37097791
-75362813
-92732163
-62378631
-73101996
-53838971
-69587672
-20567356
-42877851
-57101803
-62146312
-64184386
-6339851
-57935937
-55319705
-19904225
-90504632
-87909544
+4465
+54775
+80323
+98744
+96145
+66804
+32668
+50737
+48329
+62966
+40810
+34869
+97616
+25973
+67984
+11239
+83378
+47474
+29769
+82813
+49096
+48787
+45831
+21829
+27675
+22566
+18940
+84729
+64790
+3741
+24246
+92907
+81679
+53308
+86487
+12720
+43584
+34799
+33021
+94946
+52383
+83382
+10366
+85227
+36656
+57686
+22340
+12443
+18001
+98780
+36301
+47994
+29625
+57631
+32643
+74247
+42134
+52919
+429
+89434
+69286
+62941
+22916
+50080
+3923
+92697
+51530
+69695
+47518
+78093
+77420
+94912
+23166
+5449
+30015
+86879
+51427
+51313
+76001
+98121
+70235
+64897
+9184
+2603
+82725
+1277
+31146
+71753
+60126
+41883
+25584
+23059
+86765
+50068
+18473
+24684
+12992
+53804
+6960
+51272
assignments/3-solution.md
@@ -10,6 +10,12 @@ Chapter 6:
 pseudo-ops) that reads in a series of integers, one at a time, and outputs the
 largest and smallest values. The input will consist of a list of integer values
 
+main.c
+```c
+!include assignments/3/main.c
+```
+
+main.s
 ```assembly
 !include assignments/3/main.s
 ```
3431709-assignment-3.pdf
Binary file