Page 1 of 2

Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 1:45 pm
by Smith1776
Hello everyone. I'm working on a computer science assignment and am having trouble making progress truly understanding assembly language.
Using the C Programming language, write four version of a function that contain a loop. Each function should accept two numbers and calculate the sum of all numbers between the first number and last number (inclusive of the first and last number). Tip: Try to use the same number of variables and almost the same logic when writing the C code for all four functions. Once each function is written and tested for correct output, generate an assembly language version of the function using the command: $gcc -O1 -S filename.c. (where filename.c is the C program containing the function). Next, analyze and compare the assembly language version of each function. [50 marks]
a. Write a version of the function using a for loop
b. Write a version of the function using a while loop
c. Write a version of the function using a do..while loop
d. Write a version of the function using a goto loop
e. Is the assembly language version of each loop function the same or different? If different,
identify the differences. Your comparison should be based on:
 Number of registers used
 Number of jumps (iterations)
 Total number of operations
I managed to stumble my way through parts a to d, but am having trouble completing part e. My understanding of the underlying mechanics of assembly just isn't good enough to conduct such an analysis.

Can anyone point me to resources or methodologies for really effectively learning assembly? Getting frustrated. Thanks!

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 1:56 pm
by Mark Leavy
What hardware is the assembly language for? Modern assembly language is tough to grasp if you don't already understand how it works on simple 8 bit microprocessors.

If you really have no experience in assembly at all, then you will learn a ton by buying a simple dev board for a 6502 or a Z80 and stepping through some programs that flash LED's or turn motors on and off. Once you have the concepts down, then the more modern assembly might be more accessible. The new stuff is fairly sophisticated and not really intended for people to write programs directly in assembly. I compare it to trying to work on a modern car. Much much trickier than when I maintained my old '65 Ford.

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 2:01 pm
by Smith1776
Mark Leavy wrote: Tue Nov 10, 2020 1:56 pm What hardware is the assembly language for? Modern assembly language is tough to grasp if you don't already understand how it works on simple 8 bit microprocessors.

If you really have no experience in assembly at all, then you will learn a ton by buying a simple dev board for a 6502 or a Z80 and stepping through some programs that flash LED's or turn motors on and off. Once you have the concepts down, then the more modern assembly might be more accessible. The new stuff is fairly sophisticated and not really intended for people to write programs directly in assembly. I compare it to trying to work on a modern car. Much much trickier than when I maintained my old '65 Ford.
Thanks for your reply. It's for x86-64.

My biggest issue is the lack of a good feedback mechanism. My course is all textbook stuff, and I'm much more of a hands-on person. I like being able to play around with a system and see how it responds. Thanks for the recommendation. I will look into it.

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 2:07 pm
by Mark Leavy
Cool. Yea, you're not going to be jumping right into understanding the code that a compiler generates for x86-64.

Do you have a debugger where you can step through the code, line at a time? If so, you can usually put it in assembly mode and step through the assembly instructions one instruction at a time. Watch every register change and every memory location change that happens while you step through and if you have the instruction manual on your lap the whole time you will eventually be able to figure out what is going on.

Without a good debugger though, it would be hopeless.

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 2:10 pm
by Cortopassi
A few MOV, STO, DEC, PUSH and POP command and you're golden!

8086 assembly language is the ONLY course I ever dropped in college. :)

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 2:59 pm
by Smith1776
Mark Leavy wrote: Tue Nov 10, 2020 2:07 pm Without a good debugger though, it would be hopeless.
Geez louise. Thank you for saying that. All I have is the textbook that has written questions. And the fact that this material isn't sinking in was making me feel so dumb. Thanks to COVID I don't even have lectures to attend. It's literally just this bloody book with its stupid written questions. I got into programming so I could, well, PROGRAM lol.

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 4:23 pm
by pp4me
Smith1776 wrote: Tue Nov 10, 2020 2:59 pm
Mark Leavy wrote: Tue Nov 10, 2020 2:07 pm Without a good debugger though, it would be hopeless.
Geez louise. Thank you for saying that. All I have is the textbook that has written questions. And the fact that this material isn't sinking in was making me feel so dumb. Thanks to COVID I don't even have lectures to attend. It's literally just this bloody book with its stupid written questions. I got into programming so I could, well, PROGRAM lol.
How can you possibly complete the assignment if you only have a textbook? You must have some access to a computer or you wouldn't be able to type in gcc and get the assembly code. If you have gcc then you must have gdb (or whatever it is, but I think that was the debugger command).

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 4:30 pm
by Smith1776
pp4me wrote: Tue Nov 10, 2020 4:23 pm
How can you possibly complete the assignment if you only have a textbook? You must have some access to a computer or you wouldn't be able to type in gcc and get the assembly code. If you have gcc then you must have gdb (or whatever it is, but I think that was the debugger command).
Right, well I do have computer access but my issue is that all of the learning material provided was pen and paper stuff in the textbook.

I can write and compile all I want, but all the instruction is from this damn book.

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 4:53 pm
by Cortopassi
I assume it is the same with assembly nowadays — I have been teaching myself Python, and the internet is just filled with zillions of examples and tutorials. I got books from the library, but I end up using snippets off websites more often to learn.

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 5:00 pm
by Smith1776
Cortopassi wrote: Tue Nov 10, 2020 4:53 pm I assume it is the same with assembly nowadays — I have been teaching myself Python, and the internet is just filled with zillions of examples and tutorials. I got books from the library, but I end up using snippets off websites more often to learn.
Indeed. It's frustrating. I'm paying all this money in tuition fees but the majority of the actual learning I'm doing is from free online resources.

The university is really just a place to get a credential.

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 6:03 pm
by Hal
Well I was amazed they are still selling the system I used back in the early 80's :o
Maybe you could get something like this? The set of books that came with it were very good.

<snip>
The MPF-1 (MicroProfessor 1) was a computer system specifically designed by Multitech (now known as Acer!) as a learning tool for use in the teaching of microprocessor, microelectronics, and control technology. In the 80’s it was sold as an Z80 CPU learning and initiation system, and believe it or not, it is still in production and sold by http://www.flite.co.uk/.
<snip>
https://www.vintagevibe.co.nz/single-bo ... ofessor-1/
http://www.1000bit.it/support/manuali/m ... manual.pdf !!!!

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 6:29 pm
by pp4me
Smith1776 wrote: Tue Nov 10, 2020 4:30 pm
pp4me wrote: Tue Nov 10, 2020 4:23 pm
How can you possibly complete the assignment if you only have a textbook? You must have some access to a computer or you wouldn't be able to type in gcc and get the assembly code. If you have gcc then you must have gdb (or whatever it is, but I think that was the debugger command).
Right, well I do have computer access but my issue is that all of the learning material provided was pen and paper stuff in the textbook.

I can write and compile all I want, but all the instruction is from this damn book.
Kind of confused.

Just what course is this? They want you to write C code and then analyze the behavior of the compiled assembly code so either this course must have taught assembly language programming or else it was a pre-requisite for the course. Otherwise it would be like taking a course in French and giving you an exercise dealing with the French translation into German without having any knowledge of the latter.

Have you actually compiled and successfully tested the C code and do you have enough knowledge of assembly language to do this?

Re: Need Help Understanding Assembly Language

Posted: Tue Nov 10, 2020 10:14 pm
by Smith1776
pp4me wrote: Tue Nov 10, 2020 6:29 pm Kind of confused.

Just what course is this? They want you to write C code and then analyze the behavior of the compiled assembly code so either this course must have taught assembly language programming or else it was a pre-requisite for the course. Otherwise it would be like taking a course in French and giving you an exercise dealing with the French translation into German without having any knowledge of the latter.

Have you actually compiled and successfully tested the C code and do you have enough knowledge of assembly language to do this?
Yes, it's kind of confusing. I should clarify.

The homework and all the course material is pretty much just from this textbook. So it's all pen and paper questions to build knowledge and whatnot. However, the assignments (the stuff we are actually handing in) is all real code. The rift between the homework (physical) and assignments (digital) is so wide that getting any of that book work to translate into coding skill is nigh impossible.

And with COVID there's no lectures for me to attend or TAs to consult.

Re: Need Help Understanding Assembly Language

Posted: Wed Nov 11, 2020 1:37 am
by whatchamacallit
I am not a programmer but thought this might help.( I passed some classes almost 20 years ago but hated them.)

Cheat sheet should help identify registers jumps and operations in assembly code I hope.

https://www.google.com/url?sa=t&source= ... 1pPW3LeZrW

Re: Need Help Understanding Assembly Language

Posted: Wed Nov 11, 2020 8:50 am
by gizmo_rat
I'd suggest breaking the problem down into chunks, then compile, then compare the compiler asm output for each incremental chunk, then compare the different loop implementations. *

So start with a simple assignment x = 1 , compile, look at the output , understand what its doing.
Then some addition
Then a for loop
Then a for loop with addition as spec'ed
Then enclose the for loop in a function and call the function

You should see how it builds up in discrete logical chunks, once you understand how each chunk works, you should be able to look at the output for each of the loops and compare as spec'ed.

* its 25 years since I did this sort of stuff at college, I've never felt the need to revisit :)

Re: Need Help Understanding Assembly Language

Posted: Wed Nov 11, 2020 11:39 am
by pp4me
Smith1776 wrote: Tue Nov 10, 2020 10:14 pm
pp4me wrote: Tue Nov 10, 2020 6:29 pm Kind of confused.

Just what course is this? They want you to write C code and then analyze the behavior of the compiled assembly code so either this course must have taught assembly language programming or else it was a pre-requisite for the course. Otherwise it would be like taking a course in French and giving you an exercise dealing with the French translation into German without having any knowledge of the latter.

Have you actually compiled and successfully tested the C code and do you have enough knowledge of assembly language to do this?
Yes, it's kind of confusing. I should clarify.

The homework and all the course material is pretty much just from this textbook. So it's all pen and paper questions to build knowledge and whatnot. However, the assignments (the stuff we are actually handing in) is all real code. The rift between the homework (physical) and assignments (digital) is so wide that getting any of that book work to translate into coding skill is nigh impossible.

And with COVID there's no lectures for me to attend or TAs to consult.
I'm still confused smith1776. Step#1 is writing the C code, compiling, and testing it, so have you done that, and do you know how to do it? If not there is no reason to even talk about the compiled assembly language.

If you are already at the point where you have the assembly language code to look at then, is it the same for each? If so, the answer to e is yes and you're all done. If not, then it's time to take a deeper look. I have no way to predict whether they will be different or not but it is possible that they will all be the same. That may have been the whole point of the exercise but it's hard to say since I don't know exactly what the course is all about.

P.S. - if you get those assembler listings feel free to post them and some of us here might be able to help you out.

Re: Need Help Understanding Assembly Language

Posted: Thu Nov 12, 2020 10:38 am
by Smith1776
pp4me wrote: Wed Nov 11, 2020 11:39 am I'm still confused smith1776. Step#1 is writing the C code, compiling, and testing it, so have you done that, and do you know how to do it? If not there is no reason to even talk about the compiled assembly language.

If you are already at the point where you have the assembly language code to look at then, is it the same for each? If so, the answer to e is yes and you're all done. If not, then it's time to take a deeper look. I have no way to predict whether they will be different or not but it is possible that they will all be the same. That may have been the whole point of the exercise but it's hard to say since I don't know exactly what the course is all about.

P.S. - if you get those assembler listings feel free to post them and some of us here might be able to help you out.
Hey there pp4me. Indeed, here's my code. I appreciate all the support and help you guys are offering! ^-^

C code:
// 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;
}

Assembly code:
.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...

Re: Need Help Understanding Assembly Language

Posted: Thu Nov 12, 2020 1:07 pm
by pp4me
Good work.

Just a quick observation for now.

Thought they might all be the same but it appears you have 2 variations in the assembly code (I used DiffMerge to compare side by side).

The goto and do-while loops are identical
The for and while loops are identical except for some label names that don't matter.

As for the C code, I think it's more common to write "answer += i" than "answer = answer + i" unless things have changed in the last 5 years. With any modern optimizing compiler I doubt it would make any difference but it will save you some typing. Otherwise looks pretty good to me.

When all is said and done the only differences you need to explain are highlighted below. The for/while is on the left and the goto/do-while on the right.

Imageobjectives for exercise program

Re: Need Help Understanding Assembly Language

Posted: Thu Nov 12, 2020 2:09 pm
by pp4me
Joe Biden told the coal miner's in Pennsylvania that they could "learn to code" if he put them out of work. How hard can it be if even a coal miner can do it?

Re: Need Help Understanding Assembly Language

Posted: Thu Nov 12, 2020 2:25 pm
by Hal
pp4me wrote: Thu Nov 12, 2020 2:09 pm Joe Biden told the coal miner's in Pennsylvania that they could "learn to code" if he put them out of work. How hard can it be if even a coal miner can do it?
Easy ;) Why our former Prime Minister called Pilots "Glorified Bus Drivers". If Aussie Bus Drivers can land 747's, then USA coal miners can code and design the next generation of processors 8)
http://www.vision.net.au/~apaterson/avi ... cument.htm

Re: Need Help Understanding Assembly Language

Posted: Thu Nov 12, 2020 3:13 pm
by pp4me
Hal wrote: Thu Nov 12, 2020 2:25 pm
pp4me wrote: Thu Nov 12, 2020 2:09 pm Joe Biden told the coal miner's in Pennsylvania that they could "learn to code" if he put them out of work. How hard can it be if even a coal miner can do it?
Easy ;) Why our former Prime Minister called Pilots "Glorified Bus Drivers". If Aussie Bus Drivers can land 747's, then USA coal miners can code and design the next generation of processors 8)
http://www.vision.net.au/~apaterson/avi ... cument.htm
If it was the other way around and they were telling me as a computer programmer I had to learn to mine coal I'd probably find that very depressing.

Re: Need Help Understanding Assembly Language

Posted: Thu Nov 12, 2020 3:36 pm
by Cortopassi
Oh, c'mon this was a non-political thread! ;)

Re: Need Help Understanding Assembly Language

Posted: Thu Nov 12, 2020 4:05 pm
by pp4me
Cortopassi wrote: Thu Nov 12, 2020 3:36 pm Oh, c'mon this was a non-political thread! ;)
Well, discussions about Assembly Language are about as interesting as watching grass grow to the average reader so what did you expect?

Re: Need Help Understanding Assembly Language

Posted: Thu Nov 12, 2020 5:34 pm
by Hal
pp4me wrote: Thu Nov 12, 2020 4:05 pm
Cortopassi wrote: Thu Nov 12, 2020 3:36 pm Oh, c'mon this was a non-political thread! ;)
Well, discussions about Assembly Language are about as interesting as watching grass grow to the average reader so what did you expect?
I would rather watch grass grow than hand code assembly again!! :o
Look what you missed out on....
https://www.youtube.com/watch?v=45ELCAVb5XQ
https://www.youtube.com/watch?v=qizt0ePFEZA

Time to check myself into a Museum.

Re: Need Help Understanding Assembly Language

Posted: Thu Nov 12, 2020 9:30 pm
by yankees60
pp4me wrote: Thu Nov 12, 2020 2:09 pm Joe Biden told the coal miner's in Pennsylvania that they could "learn to code" if he put them out of work. How hard can it be if even a coal miner can do it?


!!!!!!!!!!!!!


Does the reverse hold true? All coders can coal mine?

Vinny