the register "a1" you mentioned is actually register "al" (that's an L) because it's the lower byte of 16 bit (2 byte) register ax, it has a sister register ah which is the higher byte.
I’ve always wondered: With 8 bit computers/consoles, is the register just labelled a? There are no high or low segments and no combined segment, so there would be no prefix/suffix.
@@cerulity32k Intel 8008 and 8080 did have 8-bit registers be called A, B, C, D, E, H, and L, but it also had 16-bit pairs BC, DE, and HL. So Intel always had some kind of combined registers. On Intel 4004 registers are called r0..r15 and are combined in pairs 0P..7P. No letters.
Yeah, I assume the release build has a lot less. The multiplication and addition should be compile-time and should just spit-out the final result as a constant
Title says it all. Rust became more apparent when I started looking at the language through my x86_64 asm glasses; FYI for everyone unfamiliar with assembler is relax - asm is not going to byte you (it will) but don't let it intimidate you. I still think assembler is a must and its disregard is where many run into issue understanding what needs to happen logically in code creating an environment wrought with unnecessary hair pulling.
Instant subscriber. This is how I learned Z80, 8080, 80286, 386... and so fourth assembly languages by setting C, C++ and Pascal compilers to generate assembly output starting the the 1980s. Very strong video. Wish you had some resources listed like URL to website but I'll find it through ToC. Also, I wouldn't have thought to run my Intel cross compiled on my M1. Nice simple trick thanks to Rosetta.
youtube just read my mind... I wanted to continue learning Rust but also do some assembly programming for fun. This is exactly what I needed, thanks :D
Well done, more high level programmers need to know how this works. Not that they should write assembly but it turns our that the way you write a function greatly empowers or limits what the compiler can do with it. Favor immutable choices every opportunity you get.
I'm very grateful for this video, since I'm making a compiler for school and need to understand what assembly to generate and how to handle all these language features in assembly :) Thanks hugely for the upload, hope to see more Rust and assembly!
very cool, did some assembly years ago. what maybe is more interesting is how rust does the panicking. does every imul / add have its own panicking jump block defined? i guess the reason is to find the error line later on? it be cool to see the same code compiled with c/c++/rust
Also keep in mind that this assembly here was "without optimizations" (except a few the compiler snuck in anyway). So a release build would look very different.
@@CryZe92 yes release builds look quite different.. but you can't really teach how rust is working under the hood with a release build as it's heavily optimized.
@@chrishayuk just out of curiosity I checked the assembly code with release target. I did not expect what I saw. The compiler calculated everything on it's own and stored the number 600 directly onto the stack. 600 is result3, which means the compiler itself calculated 10*20*2 + 200. It removed all other variables entirely. Impressive
Thanks for the video. I would expect everything to be similar for c++. Did not understand how the rust specific things like borrow checker helped in the compilation.
Very cool your video about Rust, I don't know anything about Rust but I want to learn, I could create a video on how to use Rust with Arduino and ESP32
21:10 What's the point of storing eax on the stack, then doing nothing with eax and loading the value from the stack into eax again? Seems not efficient at all to me? I mean yes, we wrote let result1 ... which we want to store on the stack, but shouldn't the compiler optimize this? Am I missing something?
Im not a Rust programmer, Im a C/C++ programmer, but this video was really cool. My hate with other languages is that people use the lazy argument _"The compiler does that for me..."_ or _"The compiler is smarter than you!"_ . I dont think any machine is smarten then humans, we made then, we're better than them. Plus, knowing what you program is actually doing is great for debuging and other stuf. That was a greate video my man ;) .
i agree, i like know what's under the hood also. thanks for the kind comments. compilers are just a natural evolution of automation. we see it today with transpilation in high-level languages also
Enjoying your videos! Chris, where is your accent from? I feel like I hear a bit of Scottish, English, American, I'm assuming you have lived in many places in your life? 😃
I am very much Scottish but living in England and I certainly move around the world. Throw in Irish as well as American and you've got my accent nailed. Glad you're enjoying the vids
@@chrishayuk Ah cool, knew it was a mixed accent! By the way, just a few things I noticed when watching the video yesterday, 'al', not 'a1' is actually the lower 8-bits of the ax/eax/rax register. I love the way how Rust handles runtime errors though, only miniscule difference in performance compared to C in exchange for programs that handle things properly! Also, it is quite strange how it optimises 20 * 10 to 200 only once in the generated assembly, when it could have optimised it everywhere imul was used.
I'm sorry but I do not get one thing: why there is always diffrence of byte in stack of 4? Shouldn't 4 bites be able to represent up to 15? sorry for so base level question...
Thanks for sharing! I have a question - your autocompletion looks like magic when it suggests the entire line based on a comment or something else. Is this a VSCode plugin?
I don't understand the optimization: why does it mov eax to rsp+32, check overflow, and then mov rsp+32 back to eax, which still has the value? why does it do half the calculation to get to 400, but then optimize adding 200: if it is willing to call ahead that result1=200, why isn't it doing 2*200+200, or just straight up optimize everything to 600?
The code was compiled in debug mode, meaning that you will see unoptimized code and some strange things. Any decent compiler for any good language would just replace all that stuff with loading constant 600 if you compile it in release mode and enable optimizations.
To 1st part of question : Bcoz of **seto al**. He misunderstood AL register (lowest byte of eax) as a1 register (theres no such in x86). Seto al will set al register with overflow bit, hence eax no longer has original result of calculation. After checking overflow then mov eax, rsp+32 will fetch original result back. 2nd part: I think bcoz calculation results are stored in 3 variables, compiler need to calculate in steps so it can save the results to rsp+32 & rsp+28 etc.. Or may be there is a flag for further optimisations we can set.
@@alphaanar2651 does the C compiler also just replace it with 600? I'm just curious. Recognizing that all those variables can be eliminated is quite impressing imo
@@sudeshryan8707 according to comments, once you compile to implementation, the compiler throws out all variables and stores only the final result. It is done this way because of debug mode only
Thanks for your Awesome Contents. What do you think about Zig Programming Language? I need to see zig Web Server Performance Tests. I think it's going to be cool 😎.
i have a lot of thoughts on this.. tutorial coming soon. if you look at some of my bun videos in the mean time you'll get an idea of zig web performance. it's fast
about 2 weeks, I have tried to learn Zig & I found Zig very much fun & easy to work with. Zig explicit memory management is Really Great, you might mis manager memory, but you can test it right away. Also Zig's WebAssembly & WASI Integration is Really Awesome.
@@amanueltigistu8268 actually i was playing with Zig's webassembly the other night. i quite liked it. i didn't check the underlying wat from wasm, to see if i liked what was generated but it looked promising
@@jammy3662 no worries.. i don't wanna annoy people.. had that feedback a few times. and i don't think it's a necessary component. would rather people enjoyed the content. will be interesting to see how it effects the first 30 second drop off numbers
Hello, I was looking at your video channel. We may be helping a company that uses secure images to increase supply chain security and help cloud native development. Would you be willing to help try their software, make a video, and help show devs how to use their tools? This is not an offer, but just to start a conversation about your willingness to take on sponsorship. Please provide me with your email if you are interested. You'd have a chance to look at their technology and decide if it's the type of software that you'd be interested in covering in your channel.
the register "a1" you mentioned is actually register "al" (that's an L) because it's the lower byte of 16 bit (2 byte) register ax, it has a sister register ah which is the higher byte.
I’ve always wondered: With 8 bit computers/consoles, is the register just labelled a? There are no high or low segments and no combined segment, so there would be no prefix/suffix.
@@cerulity32k Yes, the register is called A on the 8080.
long live 8086
yeah my intel assembly sucks... i will do a tutorial on this mainly to educate myself
@@cerulity32k Intel 8008 and 8080 did have 8-bit registers be called A, B, C, D, E, H, and L, but it also had 16-bit pairs BC, DE, and HL. So Intel always had some kind of combined registers.
On Intel 4004 registers are called r0..r15 and are combined in pairs 0P..7P. No letters.
Important to note: the overflow check after the calculations will only be done in the debug/dev build. In a release build they won't be done at all.
yes, will be covering releasing builds in a later part to this
Yeah, I assume the release build has a lot less. The multiplication and addition should be compile-time and should just spit-out the final result as a constant
Title says it all.
Rust became more apparent when I started looking at the language through my x86_64 asm glasses; FYI for everyone unfamiliar with assembler is relax - asm is not going to byte you (it will) but don't let it intimidate you. I still think assembler is a must and its disregard is where many run into issue understanding what needs to happen logically in code creating an environment wrought with unnecessary hair pulling.
Instant subscriber. This is how I learned Z80, 8080, 80286, 386... and so fourth assembly languages by setting C, C++ and Pascal compilers to generate assembly output starting the the 1980s. Very strong video. Wish you had some resources listed like URL to website but I'll find it through ToC. Also, I wouldn't have thought to run my Intel cross compiled on my M1. Nice simple trick thanks to Rosetta.
thank you. glad you like
You spelled FORTH wrong.
😢
I like this format for showing your face and the code at once; it's like you are reflected in the screen:
thanks... glad you like. i like the effect also, really glad you do too
I wish we would have been friends in real life and done programming together. You are doing really good job
that's so kind, thank you :)
youtube just read my mind... I wanted to continue learning Rust but also do some assembly programming for fun. This is exactly what I needed, thanks :D
glad it helped. i plan to do an assembly tutorial at some point.
fr!
Best Rust content on internet.
you are definitely too kind but glad you're enjoying. i'm sure it's frustrating that i jump merrily between languages based on my whims
Well done, more high level programmers need to know how this works. Not that they should write assembly but it turns our that the way you write a function greatly empowers or limits what the compiler can do with it. Favor immutable choices every opportunity you get.
thank you, glad you enjoyed and agreed, it's always good to understand underneath the hood
I'm very grateful for this video, since I'm making a compiler for school and need to understand what assembly to generate and how to handle all these language features in assembly :) Thanks hugely for the upload, hope to see more Rust and assembly!
Glad it helped!
just awesome - learned more about assembly then anywhere else
thank you, glad it's useful. i enjoy showing assembly as it really shows what's going on under the hood
Thank you for sharing. Greetings from Morocco.
you're welcome and hello morocco :)
very cool, did some assembly years ago. what maybe is more interesting is how rust does the panicking. does every imul / add have its own panicking jump block defined? i guess the reason is to find the error line later on? it be cool to see the same code compiled with c/c++/rust
awesome glad you like. yeah had a similar thought about comparing with C/C++ will try and do that soon
Also keep in mind that this assembly here was "without optimizations" (except a few the compiler snuck in anyway). So a release build would look very different.
@@CryZe92 yes release builds look quite different.. but you can't really teach how rust is working under the hood with a release build as it's heavily optimized.
yes there are many runtime checks that occur only in debug mode
@@chrishayuk just out of curiosity I checked the assembly code with release target. I did not expect what I saw. The compiler calculated everything on it's own and stored the number 600 directly onto the stack. 600 is result3, which means the compiler itself calculated 10*20*2 + 200. It removed all other variables entirely. Impressive
This is amazing!!!! And so well explained!
Subscribed instantly 💪
Awesome, thank you!
really like your style man, keep up the good work
Chris you are a legend man
thank you :) , glad you're enjoying the vids
Love the (Rust) vids keep it up!
glad you like :) , more rust vids coming. i promise
Thanks for the video. I would expect everything to be similar for c++. Did not understand how the rust specific things like borrow checker helped in the compilation.
i will cover ownership and borrrow checking in a future vid
Very cool your video about Rust, I don't know anything about Rust but I want to learn, I could create a video on how to use Rust with Arduino and ESP32
please do
21:10 What's the point of storing eax on the stack, then doing nothing with eax and loading the value from the stack into eax again? Seems not efficient at all to me? I mean yes, we wrote let result1 ... which we want to store on the stack, but shouldn't the compiler optimize this?
Am I missing something?
it will optimize on release bui;d. will deep dive this more
Im not a Rust programmer, Im a C/C++ programmer, but this video was really cool. My hate with other languages is that people use the lazy argument _"The compiler does that for me..."_ or _"The compiler is smarter than you!"_ . I dont think any machine is smarten then humans, we made then, we're better than them. Plus, knowing what you program is actually doing is great for debuging and other stuf. That was a greate video my man ;) .
The compiler isn't smarter, but it is ultimately way better at not making careless mistakes.
@@taragnor that still not a excuse to not know what is happening behind the hood
i agree, i like know what's under the hood also. thanks for the kind comments. compilers are just a natural evolution of automation. we see it today with transpilation in high-level languages also
@Chris nice video, do you give courses in rust with projects?
ah no, sorry, this is really just about sharing thoughts.
Loving your content Chris. Keep it up.
thank you
great explanation
Thank you
Enjoying your videos! Chris, where is your accent from? I feel like I hear a bit of Scottish, English, American, I'm assuming you have lived in many places in your life? 😃
I am very much Scottish but living in England and I certainly move around the world. Throw in Irish as well as American and you've got my accent nailed. Glad you're enjoying the vids
@@chrishayuk Ah cool, knew it was a mixed accent! By the way, just a few things I noticed when watching the video yesterday, 'al', not 'a1' is actually the lower 8-bits of the ax/eax/rax register. I love the way how Rust handles runtime errors though, only miniscule difference in performance compared to C in exchange for programs that handle things properly! Also, it is quite strange how it optimises 20 * 10 to 200 only once in the generated assembly, when it could have optimised it everywhere imul was used.
@@nark4837 my intel assembly sucks. i will do a tutorial vid on it soon. mainly to educate myself
"Gubbins", lol. Scottish roots on show there!
hahaha, you caught me, i'm scottish through and through
I'm sorry but I do not get one thing: why there is always diffrence of byte in stack of 4? Shouldn't 4 bites be able to represent up to 15? sorry for so base level question...
Thanks for this content Chris!
you're welcome
Thanks for sharing! I have a question - your autocompletion looks like magic when it suggests the entire line based on a comment or something else. Is this a VSCode plugin?
I'm assuming it's GitHub copilot, which is a paid extension based on OpenAI's ChatGPT
@@theninjascientist689 github copilot is based on char gpt??? 🤯
I don't understand the optimization: why does it mov eax to rsp+32, check overflow, and then mov rsp+32 back to eax, which still has the value? why does it do half the calculation to get to 400, but then optimize adding 200: if it is willing to call ahead that result1=200, why isn't it doing 2*200+200, or just straight up optimize everything to 600?
The code was compiled in debug mode, meaning that you will see unoptimized code and some strange things. Any decent compiler for any good language would just replace all that stuff with loading constant 600 if you compile it in release mode and enable optimizations.
To 1st part of question :
Bcoz of **seto al**. He misunderstood AL register (lowest byte of eax) as a1 register (theres no such in x86). Seto al will set al register with overflow bit, hence eax no longer has original result of calculation. After checking overflow then mov eax, rsp+32 will fetch original result back.
2nd part:
I think bcoz calculation results are stored in 3 variables, compiler need to calculate in steps so it can save the results to rsp+32 & rsp+28 etc.. Or may be there is a flag for further optimisations we can set.
This compiles via LLVM to assembly I guess. Probably LLVM also does some optimizations, even in debug mode.
@@alphaanar2651 does the C compiler also just replace it with 600? I'm just curious. Recognizing that all those variables can be eliminated is quite impressing imo
@@sudeshryan8707 according to comments, once you compile to implementation, the compiler throws out all variables and stores only the final result. It is done this way because of debug mode only
Great video!!
thanks, glad you enjoyed :)
lovely 👍
Thank you 😊
Thanks for your Awesome Contents.
What do you think about Zig Programming Language? I need to see zig Web Server Performance Tests. I think it's going to be cool 😎.
i have a lot of thoughts on this.. tutorial coming soon. if you look at some of my bun videos in the mean time you'll get an idea of zig web performance. it's fast
about 2 weeks, I have tried to learn Zig & I found Zig very much fun & easy to work with. Zig explicit memory management is Really Great, you might mis manager memory, but you can test it right away. Also Zig's WebAssembly & WASI Integration is Really Awesome.
@@amanueltigistu8268 actually i was playing with Zig's webassembly the other night. i quite liked it. i didn't check the underlying wat from wasm, to see if i liked what was generated but it looked promising
@@chrishayuk I can't wait to see the tutorial Video for Zig. I think it's going to be very much cool. Thanks.
I just installed rust recently but not started practicing. Still Learning Java(just started)
learn multiple languages, it gets easier
👍
0:50 : the mixing of music is *bad*, it shouldn't be so loud 😕
agreed, fixed for new vids
pretty
I'm actually surprised it didn't just mov eax, 600.
It does if you turn on compiler optimization, which is off by default. Add "-C opt-level=1". Works on Godbolt.
i have the sa issue..
How's it going? Did you learn how to use it?
reverse engineering
i dont write rust or assembly. why am i watching this 😂. very good explanation tho
it's always fun to learn new stuff, will improve how you approach your existing languages. glad you enjoyed it
The color is really strange.
I like to experiment, I’m partial to the smurf / avatar vibe
Please reverse engineering the ransomware in rust
that intro is WAY too loud
yeah. sorry. i've ditched the intro for my latest video. will see how that compares and will rethink
@@chrishayuk thanks for being open to feedback!
@@jammy3662 no worries.. i don't wanna annoy people.. had that feedback a few times. and i don't think it's a necessary component. would rather people enjoyed the content. will be interesting to see how it effects the first 30 second drop off numbers
Where did you get your code completion? it looks really useful
GitHub copilot
@@chrishayuk thanks!
Hello,
I was looking at your video channel. We may be helping a company that uses secure images to increase supply chain security and help cloud native development. Would you be willing to help try their software, make a video, and help show devs how to use their tools?
This is not an offer, but just to start a conversation about your willingness to take on sponsorship. Please provide me with your email if you are interested.
You'd have a chance to look at their technology and decide if it's the type of software that you'd be interested in covering in your channel.
hey thanks for the reach out, but sponsorship isn't really a direction i'm going in, but appreciate the thought