Get under the hood of Rust Language with Assembly!! Rust Programming Tutorial

Поделиться
HTML-код
  • Опубликовано: 23 окт 2024

Комментарии • 122

  • @dyslexicsteak897
    @dyslexicsteak897 2 года назад +52

    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.

    • @cerulity32k
      @cerulity32k 2 года назад +6

      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.

    • @cmilkau
      @cmilkau 2 года назад +7

      @@cerulity32k Yes, the register is called A on the 8080.

    • @cmilkau
      @cmilkau 2 года назад +1

      long live 8086

    • @chrishayuk
      @chrishayuk  2 года назад +14

      yeah my intel assembly sucks... i will do a tutorial on this mainly to educate myself

    • @rsa5991
      @rsa5991 2 года назад +1

      @@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.

  • @bestformspielt
    @bestformspielt 2 года назад +41

    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.

    • @chrishayuk
      @chrishayuk  2 года назад +2

      yes, will be covering releasing builds in a later part to this

    • @stephenJpollei
      @stephenJpollei 2 года назад +2

      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

  • @NOPerative
    @NOPerative 10 месяцев назад +1

    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.

  • @richsadowsky8580
    @richsadowsky8580 2 года назад +11

    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.

  • @SimonClarkstone
    @SimonClarkstone 2 года назад +4

    I like this format for showing your face and the code at once; it's like you are reflected in the screen:

    • @chrishayuk
      @chrishayuk  2 года назад +3

      thanks... glad you like. i like the effect also, really glad you do too

  • @_realist_6957
    @_realist_6957 2 года назад +8

    I wish we would have been friends in real life and done programming together. You are doing really good job

    • @chrishayuk
      @chrishayuk  2 года назад +1

      that's so kind, thank you :)

  • @SandwichMitGurke
    @SandwichMitGurke 2 года назад +4

    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

    • @chrishayuk
      @chrishayuk  2 года назад +2

      glad it helped. i plan to do an assembly tutorial at some point.

    • @YourMom-rg5jk
      @YourMom-rg5jk Год назад

      fr!

  • @kaplansedat
    @kaplansedat 2 года назад +4

    Best Rust content on internet.

    • @chrishayuk
      @chrishayuk  2 года назад

      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

  • @ntippy
    @ntippy 2 года назад +6

    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.

    • @chrishayuk
      @chrishayuk  2 года назад

      thank you, glad you enjoyed and agreed, it's always good to understand underneath the hood

  • @spaghettiking653
    @spaghettiking653 2 года назад +1

    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!

  • @cd-stephen
    @cd-stephen Год назад +1

    just awesome - learned more about assembly then anywhere else

    • @chrishayuk
      @chrishayuk  Год назад +1

      thank you, glad it's useful. i enjoy showing assembly as it really shows what's going on under the hood

  • @proudmoroccan8164
    @proudmoroccan8164 2 года назад +2

    Thank you for sharing. Greetings from Morocco.

    • @chrishayuk
      @chrishayuk  2 года назад +1

      you're welcome and hello morocco :)

  • @EudaderurScheiss
    @EudaderurScheiss 2 года назад +10

    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

    • @chrishayuk
      @chrishayuk  2 года назад +2

      awesome glad you like. yeah had a similar thought about comparing with C/C++ will try and do that soon

    • @CryZe92
      @CryZe92 2 года назад +6

      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.

    • @chrishayuk
      @chrishayuk  2 года назад +2

      @@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.

    • @dyslexicsteak897
      @dyslexicsteak897 2 года назад +1

      yes there are many runtime checks that occur only in debug mode

    • @SandwichMitGurke
      @SandwichMitGurke 2 года назад +3

      @@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

  • @ricardorosa5315
    @ricardorosa5315 Год назад +1

    This is amazing!!!! And so well explained!
    Subscribed instantly 💪

  • @valentinomelis8331
    @valentinomelis8331 2 года назад +2

    really like your style man, keep up the good work

  • @_realist_6957
    @_realist_6957 2 года назад +2

    Chris you are a legend man

    • @chrishayuk
      @chrishayuk  2 года назад +2

      thank you :) , glad you're enjoying the vids

  • @DavidEngelen
    @DavidEngelen 2 года назад +2

    Love the (Rust) vids keep it up!

    • @chrishayuk
      @chrishayuk  2 года назад +1

      glad you like :) , more rust vids coming. i promise

  • @sanket1729
    @sanket1729 2 года назад +1

    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.

    • @chrishayuk
      @chrishayuk  2 года назад +1

      i will cover ownership and borrrow checking in a future vid

  • @alexon2010
    @alexon2010 2 года назад +2

    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

  • @SandwichMitGurke
    @SandwichMitGurke 2 года назад +3

    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?

    • @chrishayuk
      @chrishayuk  2 года назад

      it will optimize on release bui;d. will deep dive this more

  • @EnderMega
    @EnderMega 2 года назад +3

    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 ;) .

    • @taragnor
      @taragnor 2 года назад +3

      The compiler isn't smarter, but it is ultimately way better at not making careless mistakes.

    • @EnderMega
      @EnderMega 2 года назад +1

      @@taragnor that still not a excuse to not know what is happening behind the hood

    • @chrishayuk
      @chrishayuk  2 года назад +3

      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

  • @ahmadumar6210
    @ahmadumar6210 2 года назад +1

    @Chris nice video, do you give courses in rust with projects?

    • @chrishayuk
      @chrishayuk  2 года назад +2

      ah no, sorry, this is really just about sharing thoughts.

  • @falcon20243
    @falcon20243 2 года назад +1

    Loving your content Chris. Keep it up.

  • @romandzhadan5546
    @romandzhadan5546 Год назад +1

    great explanation

  • @nark4837
    @nark4837 2 года назад +2

    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? 😃

    • @chrishayuk
      @chrishayuk  2 года назад +2

      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

    • @nark4837
      @nark4837 2 года назад +2

      @@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.

    • @chrishayuk
      @chrishayuk  2 года назад +1

      @@nark4837 my intel assembly sucks. i will do a tutorial vid on it soon. mainly to educate myself

  • @uilleachan
    @uilleachan 2 года назад +2

    "Gubbins", lol. Scottish roots on show there!

    • @chrishayuk
      @chrishayuk  2 года назад +2

      hahaha, you caught me, i'm scottish through and through

  • @anfitanin
    @anfitanin 2 года назад

    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...

  • @krome305
    @krome305 2 года назад +1

    Thanks for this content Chris!

  • @nikitablack1568
    @nikitablack1568 Год назад

    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?

    • @theninjascientist689
      @theninjascientist689 Год назад

      I'm assuming it's GitHub copilot, which is a paid extension based on OpenAI's ChatGPT

    • @rizkiaprita
      @rizkiaprita Год назад

      @@theninjascientist689 github copilot is based on char gpt??? 🤯

  • @eliavrad2845
    @eliavrad2845 2 года назад +3

    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?

    • @alphaanar2651
      @alphaanar2651 2 года назад +4

      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.

    • @sudeshryan8707
      @sudeshryan8707 2 года назад +4

      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.

    • @markjans2169
      @markjans2169 2 года назад +1

      This compiles via LLVM to assembly I guess. Probably LLVM also does some optimizations, even in debug mode.

    • @SandwichMitGurke
      @SandwichMitGurke 2 года назад

      @@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

    • @jackgerberuae
      @jackgerberuae 2 года назад +2

      @@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

  • @stevenreina5774
    @stevenreina5774 2 года назад +1

    Great video!!

    • @chrishayuk
      @chrishayuk  2 года назад +1

      thanks, glad you enjoyed :)

  • @schoudhary108
    @schoudhary108 2 года назад +1

    lovely 👍

  • @amanueltigistu8268
    @amanueltigistu8268 2 года назад +1

    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 😎.

    • @chrishayuk
      @chrishayuk  2 года назад +1

      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

    • @amanueltigistu8268
      @amanueltigistu8268 2 года назад +1

      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.

    • @chrishayuk
      @chrishayuk  2 года назад +2

      @@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

    • @amanueltigistu8268
      @amanueltigistu8268 2 года назад

      @@chrishayuk I can't wait to see the tutorial Video for Zig. I think it's going to be very much cool. Thanks.

  • @coolkats9331
    @coolkats9331 2 года назад +1

    I just installed rust recently but not started practicing. Still Learning Java(just started)

    • @chrishayuk
      @chrishayuk  2 года назад +1

      learn multiple languages, it gets easier

  • @guilherme5094
    @guilherme5094 2 года назад +1

    👍

  • @leyasep5919
    @leyasep5919 2 года назад +1

    0:50 : the mixing of music is *bad*, it shouldn't be so loud 😕

    • @chrishayuk
      @chrishayuk  2 года назад +1

      agreed, fixed for new vids

  • @hezuikn
    @hezuikn 2 года назад +1

    pretty

  • @cmilkau
    @cmilkau 2 года назад +1

    I'm actually surprised it didn't just mov eax, 600.

    • @DeusGladiorum
      @DeusGladiorum 2 года назад +3

      It does if you turn on compiler optimization, which is off by default. Add "-C opt-level=1". Works on Godbolt.

  • @victorpaulo4342
    @victorpaulo4342 2 года назад

    i have the sa issue..

  • @porterrucki5479
    @porterrucki5479 2 года назад +1

    How's it going? Did you learn how to use it?

  • @rizkiaprita
    @rizkiaprita Год назад +1

    i dont write rust or assembly. why am i watching this 😂. very good explanation tho

    • @chrishayuk
      @chrishayuk  Год назад +1

      it's always fun to learn new stuff, will improve how you approach your existing languages. glad you enjoyed it

  • @qscuio
    @qscuio Год назад +1

    The color is really strange.

    • @chrishayuk
      @chrishayuk  Год назад

      I like to experiment, I’m partial to the smurf / avatar vibe

  • @raihanrafi3665
    @raihanrafi3665 2 месяца назад

    Please reverse engineering the ransomware in rust

  • @jammy3662
    @jammy3662 2 года назад +1

    that intro is WAY too loud

    • @chrishayuk
      @chrishayuk  2 года назад

      yeah. sorry. i've ditched the intro for my latest video. will see how that compares and will rethink

    • @jammy3662
      @jammy3662 2 года назад +1

      @@chrishayuk thanks for being open to feedback!

    • @chrishayuk
      @chrishayuk  2 года назад

      @@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

  • @leave-a-comment-at-the-door
    @leave-a-comment-at-the-door Год назад +2

    Where did you get your code completion? it looks really useful

  • @IleniaQuintero
    @IleniaQuintero 5 месяцев назад

    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.

    • @chrishayuk
      @chrishayuk  5 месяцев назад

      hey thanks for the reach out, but sponsorship isn't really a direction i'm going in, but appreciate the thought