i did the easiest LeetCode in assembly

preview_player
Показать описание
TODAY I TAKE ON A MIGHTY CHALLENGE. A QUEST THAT NO OTHER PROGRAMMER OR CONTENT CREATOR WILL TAKE ON FOR THEIR VIEWERS.

Today I will do the LeetCode challenge TwoSum, but that challenge is TWO EASY. TODAY, WE WILL BE DOING THAT PROBLEM IN ASSEMBLY. FOLLOW ALONG WITH ME ON MY QUEST.

Wish me luck.

🏫 COURSES 🏫

🔥🔥🔥 SOCIALS 🔥🔥🔥
Рекомендации по теме
Комментарии
Автор

such a interesting video... never would I in a million years expect someone to do twosum in assembly. great job!

TigerAnimationsYT
Автор

I still have nightmares of assembly almost 20 yrs ago on masm32 staying with C. Respects you make it look so easy.

lykewize
Автор

interviewer: can you solve this problem?

no one: sure, my language of choice is assembly

danmuji
Автор

Very, very impressive, and excellent to see some real(!!) assembly skills on youtube. You would have liked the 80's and 90's 'golden era' for assembly coders, certainly from a game making perspective. Things have certainly moved far with CPU instruction sets, but reassuring to see so much familiarity with those opcodes and concepts. Subbed.

ChrisM
Автор

this is awesome, really makes me want to learn assembly. You make it look so trivial, for me it would be a nightmare!!

Jackarius
Автор

I love assembly! And I loved your cheeky online assembly work around.
Did you get assembly in college? I am amazed that most 30 something and below colleagues never ever did anything in assembly. I think I was the last generation where we did a hell of a lot of assembly. Mainly Z80 and 8086 and 68000. I studied EE (digital electronics) and back in 1990 you didn’t have Arduino and cool ESP32s that you could program in C. So when we designed an SBC, we also needed to make it do a certain task and that was basically done in assembly. Was the quickest and easiest way to iterate.

CallousCoder
Автор

Now I remember how I used to look at code with 0 programming experience

szilagyimiklos
Автор

never seen someone use "ii" for the inner loop haha, very cool though! I'll pretend I followed most of it 🙃

ac_trainr_blak
Автор

Love this - pleased to see younger developers with such enthusiasm for lower level development skills

liberatemi
Автор

When accessing pointers multiple times, it helps to store the pointer in a temporary register (if you need it from the stack) and then doing your "lea" operations as needed. The "lea rbx, [rbx..]" method works fine if you only need to access it once. But in this case, it would've been better to not clobber rbx and instead store the first result into another temporary register, removing the need to get setup rbx by accessing the stack again for the second result. As well, there were still plenty of registers available (r10-r15) so using the stack wasn't necessary in this case.

Bittboy
Автор

You should post the code as "easy to understand and fast solution"

hampuztt
Автор

Amazing video! Doing ASM this semester and loving it. We are doing dbl precision fp and bit wise ops right now. Just got done creating custom functions and c function calls. Love this class it’s fun.

criostasis
Автор

This is pretty cool, though if you wanted to make it easier to follow I'd do it outline instead and get syntax highlighting. I prefer nasm syntax over gas Intel, but at least you didn't do it with AT&T syntax. That would've been the worst.

anon_y_mousse
Автор

Assembly is alien to me. However, I appreciate what you did here.

PinakiGuptaAppu
Автор

The main thing to learn here is that C inline assembly syntax and boilerplate is terrible.
One of the only good reasons to learn pascal is that freepascal does inline assembly (mostly) right.
Big ups for torturing yourself into getting used to this hell.

That being said, here are my ASM nitpicks:
Mov'ing into ebx just to compare eax and ebx when you scratch ebx after compare instead of doing 'cmp eax, dword ptr [rsp+0x20]'
Using 'inc' instead of adding 1 tends to be slightly slower on some CPUs, specifically some intel CPUs.
Using dword mov operations while you allocate the full 64 bytes anyway - you are saving a bit of instruction cache when you should be saving instruction cache AND memory cache. If rsp isn't aligned to cache boundaries (and it often won't be) you might lose some cache just to accessing your arguments instead of reserving it for an already cache heavy array search operation.
While XORing a register to itself does often cancel during instruction parsing, in my experience it sometimes won't (for whatever reason) so it's usually best practice to xor dwords even on higher registers (I think it has something to do with μ-instruction hardware often wanting to combine successive instructions but I'm not certain exactly where the slowdown comes from).

I don't just have critique, I really like how you did 'lea rax, [rax+4]' even though 'add rax, 4' is less complex and fewer letters because it makes it completely clear without a comment that rax is supposed to be a memory pointer, not a normal value. You also did 'cmp' to memory addresses in multiple places so my above nitpick is clearly of a freak occurrence.

icarvs_vivit
Автор

when i think too much about how computers work it hurts my head, great work!

KevinNaughtonJr
Автор

This video was more informative than my professor who taught for a semester

g-nonymousgems
Автор

Employer: so what is your name?
LLL: I solve leet code problems in asm
Employer: Hired

MahmoudAgha
Автор

the __attribute<naked> trick was really interesting - I wonder if it would work to integrate a C function into some asm code with non-standard calling conventions... and reminds me of another question:

is there a way to dynamically parse C structures into nasm files in a mixed project so that the structures can change as needed by the expanding C codebase without having to rewrite asm code that also uses them?

also, had a quick go at the O(n) solution in C, looks like it would be easy enough in asm too...

also also, why put input values on the stack? you had enough registers to not need the stack at all except for storing registers that can't be clobbered, and then you wouldn't have needed to keep pulling those values back from memory...


also also also, thinking now about the extension to this problem where there is a list of targets to find pairs for in the same dataset, maybe has to be sorted lists for reasonable optimisation...

treelibrarian
Автор

Can you do some harder problem in assembly? You did easy one and my mind already blown

gtthwzc