r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

10

u/Alexrai123 Apr 05 '22

Assembly language. What. The. Fuck. 50 lines to transform 'a' to 'A'.

11

u/MrSloppyPants Apr 05 '22

All higher level languages are compiled down to assembler and then machine code. Assembler is almost exactly how the CPU actually executes code. A CPU has no idea what a Python statement is, a CPU needs to know the specific OpCode of the instruction you wish to execute, what data needs to be pushed into the appropriate registers, where the stack pointer is, where the instruction pointer is, and more. Taking a compiler course to understand how computer instruction sets actually work is very beneficial

1

u/srethrowaway808 Apr 06 '22

Can you recommend a compiler course?

3

u/shine_on Apr 05 '22

Computers, deep down inside, can only do a handful of simple things. They can fetch a value from memory, they can store a value in memory, they can add two numbers, they can check if the result of the last operation was zero, things like that. Although they can do these things extremely quickly, it's very tedious to write a large amount of code in this way. Higher level programming languages were invented as a means to make large programs more understandable to us humans, but ultimately every line of C or Java or Python is translated to several lines of assembly language, because that's what the processor understands.

2

u/flank-cubey-cube Apr 05 '22 edited Apr 05 '22

For your specific example, I'm not sure why the following wouldn't work. Also, this is assuming you're using an array of chars as the string. Another reason is if we used a string constant, we wouldn't be able to actually modify the string. For our example, we will be using the following the C function.

#include <stdio.h>

int main() {
    char str[] = "abc";
    str[0] = "A";
}

Arrays in assembly just look like local variables, but contiguous. The other thing note worthy is that the first element of the array will be at the topmost position in the stack. In other words, it will be be closes to the stack pointer (rsp in our case).

push rbp           ;function prologue
mov rbp, rsp       ; function prologue
sub rsp, 0x20      ; allocating space for local variables

mov [rbp - 20], 97 ; moving 'a' (ascii representation) to the top most
                   ; point in the stack - str[0] = 'a'
mov [rbp - 18], 98 ; 'b' into str[1] = 'b'
mov [rbp - 10], 99 ; 'c' intro str[2] = 'c'

; To change the value, the value at str[0], we need to modify what's at ; [rbp - 20]

mov [rbp - 20], 65 ; 'A' is 65 - changing 'a' to 'A'

add rsp, 0x20 ; function epilogue
ret           ; function epilogue

Some diclosure -- this isn't exactly what you'd see. You'd probably see a string defined from another section moved into somewhere on the local stack. But what's happening is essentially the same.

The hardest part about assembly is actually not the code like your comment suggests, it's the compiler quirks (Different ABIs and optimization wizardly) and the linker/loader magic that makes it hard.

2

u/nannerz_ Apr 06 '22

Im literally taking a break from programming in assembly to scroll through Reddit lmao. But I’m doing it for a class (my last programming class of college!) and probably won’t ever do it again

1

u/Alexrai123 Apr 06 '22

Same, doing it for college, deff not going to touch that thing again

2

u/otr_trucker Apr 06 '22

IMO, to understand assembly you first need to understand how a cpu works. A simple CPU has little internal memory areas called registers. These registers are only a few bits long. An 8 bit cpu would only have a register that is 8 bit long. Some registers may be able to be paired together to get double the space. The cpu can move data into and out of the registers. It can also preform some logical operations on certain registers (add, sub, AND, etc) and store the results. The cpu knows what to do based on machine code that is the program. Machine code is simply a series of 1s and 0s. One of the registers is called a program counter (PC). The PC register holds the memory address for the next instruction. A cycle of operation would be for the cpu to put the value of the PC register onto the address bus then load the value found at that address into an instruction register. It would then increment the PC then use the value in the instruction register to turn on or off control lines that cause the cpu to preform an operation according to that value.

Now we get to assembly. Assembly is just a step away from writing code in machine code. Instead of writing 1s and 0s you get to use a little bit easier to read code like MOV or ADD. But you are still talking to the CPU on its most primitive level. To preform an operation like ADD, you must first tell it to get a value from memory and put it in a register. Then you have to get another value from memory and put in another register. Now you can add the 2 registers together. Then you have to move the result from its location back into memory. A higher level language would handle all those steps for you but in assembly you have to do it all yourself.

To complicate things different cpus will have different registers and they will different commands. So when you talk about assembly, you generally would need to talk about which cpu you are talking about. An Intel cpu would be different from an ARM cpu and thus the code will be different for each.

At this point you are probably wondering why anyone would use assembly. The reason is that the closer you get to machine code the more power you have over your program, but the more work you have to put into it. Python is very easy to code in when compared to assembly. But python has rules about how you do things, and for the vast majority of your coding that is fine. But a python program will be larger in memory and will run slower than an assembly program that does the same thing. For a program that is to be run on a top of its class gaming PC, this isn't a problem. But, when you are looking at an embedded system, you are usually looking at limited resources and you may not have the space or speed necessary to use python. You can do things in assembly you can't do in python.

I've always thought that python is like riding around in a chauffeur driven car. C++ is like driving the car yourself. With assembly, you first have to build the car before you can drive it.

I suggest you watch Ben Eater's YouTube series on building an 8bit cpu on breadboards to get a better understand of how a cpu works.

0

u/[deleted] Apr 05 '22

[deleted]

1

u/ShelZuuz Apr 06 '22

That's apples to oranges. ToUpper is a library function. You can call library functions from assembler if you want.

You have to compare doing an uppercase transform of a letter in assembly to doing one in a higher level language without any library function.