Hey there pp4me. Indeed, here's my code. I appreciate all the support and help you guys are offering!
// Importing generic libraries for the project
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Declaration of functions used in the project corresponding to loops required by Step 2
int for_loop(int x, int y);
int while_loop(int x, int y);
int do_while_loop(int x, int y);
int go_to_loop(int x, int y);
// Main function
int main() {
int x = 25;
int y = 50;
for_loop(x, y);
while_loop(x, y);
do_while_loop(x, y);
go_to_loop(x, y);
}
int for_loop(int x, int y) {
int answer = 0;
for(int i = x; i <= y; i++) {
answer = answer + i;
}
printf("%d",answer);
return answer;
}
int while_loop(int x, int y) {
int answer = 0;
while (x <= y) {
answer = answer + x;
x++;
}
printf("%d",answer);
return answer;
}
int do_while_loop(int x, int y) {
int answer = 0;
do {
answer = answer + x;
x++;
} while (x <= y);
printf("%d",answer);
return answer;
}
int go_to_loop(int x, int y) {
int answer = 0;
restart_loop:
answer = answer + x;
x++;
if (x <= y) {
goto restart_loop;
}
printf("%d",answer);
return answer;
}
.section __TEXT,__text,regular,pure_instructions
.build_version macos, 10, 15 sdk_version 10, 15, 4
.globl _main ## -- Begin function main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
movl $25, %edi
movl $50, %esi
callq _for_loop
movl $25, %edi
movl $50, %esi
callq _while_loop
movl $25, %edi
movl $50, %esi
callq _do_while_loop
movl $25, %edi
movl $50, %esi
callq _go_to_loop
xorl %eax, %eax
popq %rbp
retq
.cfi_endproc
## -- End function
.globl _for_loop ## -- Begin function for_loop
.p2align 4, 0x90
_for_loop: ## @for_loop
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
pushq %rbx
pushq %rax
.cfi_offset %rbx, -24
## kill: def $edi killed $edi def $rdi
xorl %ebx, %ebx
cmpl %esi, %edi
jg LBB1_2
## %bb.1:
cmpl %edi, %esi
cmovll %edi, %esi
movl %esi, %eax
subl %edi, %eax
movl %edi, %ecx
notl %ecx
addl %esi, %ecx
leal 1(%rdi), %ebx
imull %eax, %ebx
imulq %rcx, %rax
shrq %rax
addl %edi, %ebx
addl %eax, %ebx
LBB1_2:
leaq L_.str(%rip), %rdi
movl %ebx, %esi
xorl %eax, %eax
callq _printf
movl %ebx, %eax
addq $8, %rsp
popq %rbx
popq %rbp
retq
.cfi_endproc
## -- End function
.globl _while_loop ## -- Begin function while_loop
.p2align 4, 0x90
_while_loop: ## @while_loop
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
pushq %rbx
pushq %rax
.cfi_offset %rbx, -24
## kill: def $edi killed $edi def $rdi
xorl %ebx, %ebx
cmpl %esi, %edi
jg LBB2_2
## %bb.1:
cmpl %edi, %esi
cmovll %edi, %esi
movl %esi, %eax
subl %edi, %eax
movl %edi, %ecx
notl %ecx
addl %esi, %ecx
leal 1(%rdi), %ebx
imull %eax, %ebx
imulq %rcx, %rax
shrq %rax
addl %edi, %ebx
addl %eax, %ebx
LBB2_2:
leaq L_.str(%rip), %rdi
movl %ebx, %esi
xorl %eax, %eax
callq _printf
movl %ebx, %eax
addq $8, %rsp
popq %rbx
popq %rbp
retq
.cfi_endproc
## -- End function
.globl _do_while_loop ## -- Begin function do_while_loop
.p2align 4, 0x90
_do_while_loop: ## @do_while_loop
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
pushq %rbx
pushq %rax
.cfi_offset %rbx, -24
## kill: def $edi killed $edi def $rdi
cmpl %edi, %esi
cmovll %edi, %esi
movl %edi, %eax
notl %eax
addl %esi, %eax
movl %esi, %ecx
subl %edi, %ecx
leal 1(%rdi), %ebx
imull %ecx, %ebx
imulq %rax, %rcx
shrq %rcx
addl %edi, %ebx
addl %ecx, %ebx
leaq L_.str(%rip), %rdi
movl %ebx, %esi
xorl %eax, %eax
callq _printf
movl %ebx, %eax
addq $8, %rsp
popq %rbx
popq %rbp
retq
.cfi_endproc
## -- End function
.globl _go_to_loop ## -- Begin function go_to_loop
.p2align 4, 0x90
_go_to_loop: ## @go_to_loop
.cfi_startproc
## %bb.0:
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
pushq %rbx
pushq %rax
.cfi_offset %rbx, -24
## kill: def $edi killed $edi def $rdi
cmpl %edi, %esi
cmovll %edi, %esi
movl %edi, %eax
notl %eax
addl %esi, %eax
movl %esi, %ecx
subl %edi, %ecx
leal 1(%rdi), %ebx
imull %ecx, %ebx
imulq %rax, %rcx
shrq %rcx
addl %edi, %ebx
addl %ecx, %ebx
leaq L_.str(%rip), %rdi
movl %ebx, %esi
xorl %eax, %eax
callq _printf
movl %ebx, %eax
addq $8, %rsp
popq %rbx
popq %rbp
retq
.cfi_endproc
## -- End function
.section __TEXT,__cstring,cstring_literals
L_.str: ## @.str
.asciz "%d"
.subsections_via_symbols
In terms of the number of registers used and operations executed they look fairly similar to me.
The conclusion I have come to is that the first two loops seem to iterate 26 times while the next two loops iterate 25 times. The first two loops subsequently seem to have 28 assembly operations each while the next two loops execute 26 assembly operations each. The reason for the discrepancy being that the latter two loops execute their first increment operation before checking their loop conditionality. That's the way I see it so far anyway...