Yul is a low-level assembly language used in Solidity smart contracts that serves as an intermediary between high-level Solidity code and compiled EVM bytecode, providing developers with deeper understanding of how Solidity works, enabling more efficient gas usage, and allowing access to new language features before they appear in Solidity. Unlike Solidity, Yul has only one data type (a 32-byte word), no storage variables, no automatic memory management, no arrays, and no function argument parsing, requiring developers to manually handle all operations at the EVM level.
Yul Assembly in Solidity: EVM Smart Contract Programming
Added:greetings and welcome to another reinblock screencast in this screencast I'm going to give you an introduction to y'all which is a Assembly Language that is used in solidity in fact it is an intermediary language that sits between the solidity code and the compiled byte code so let me give you a quick intro as to what Yule is and when you can use it and why you use it as well as what the benefits are for using it so basically if you use uh you'll you'll get a better understanding of how the solidity language works and it will help you to debug and solve problems in your solidity programming code experience so sometimes you know when you're programming in solidity you can be guided by the compiler errors and this is all well and good but you need to know what what is driving those errors and why that is happening and why you're seeing those errors and assembly by going into assembly will help you understand that and help you get a deeper understanding as to how solidity behaves the way it does for example you might have some questions like why is it that you can't do a kekak 256 of 50 directly but you can do it if you pass in the ABI encode of 50. or for example why are you unable to append to a raise in memory but you kind of tend to raise in storage so these kind of questions can be answered once you get a familiarity with the your programming language so what are some of the benefits of learning how to program a mule well there are a couple that come to mind one of them is that when solidity is upgraded the new changes in the language are often only initially available in your and so this might be of benefic benefits to developers who want to take advantage of these new apis as early as possible and the other thing that is of benefit is contracts written in Yule or functions when in your can often have more efficient gas usage and so you can use your either directly in your contract in an assembly block or you can actually write your contracts 100 if you want to and these contracts as I mentioned very often are much more gas efficient now this doesn't come without some kind of finally what are the basics of your well this slide here doesn't cover all the basics but it does cover some of the things that you should be aware of and the fact that you all is a very basic language in the sense that it doesn't provide all the syntactic sugar and the helpers that you're used to when writing contracts in solidity so what you'll does not have is it does not have storage variables it does not manage memory for you it does not have a raise it does not pass function arguments and it does not provide that many data types well actually it only provides one data type and so what we're going to do in the next section is we'll open up an editor and we'll start playing around with you all and I will start off with looking at the one available data type that we can use in your programming so I'm going to show you a basic example here of types in solidity and then in your so the first type we're going to look at is basically a number a uint 256 which is the type that you've probably used quite a lot in solidity and so what I'll do is I create a function here in pure solidity first of all that will return you into 56. so the way you do this in solidity is you write the function and then you give the function a name so let's just call it number type and then we make this an external pure function which Returns the un256 units 256 like that and then in the function itself we can define a unit 256.
and we can call it x equals and we call give it a value of 99 and then we can return X now I've got this to automatically compile so that's already compiled no errors let's deploy the contract and let's expand out the contract that we've deployed so it's uh and we can call the function we can see it's got 99 which is this now how do we convert this to use an assembly Block in your so that we can return value X well let's get rid of everything except the definition of X in solidity and then we can write an assembly block like so and we can actually assign the variable X inside the assembly block using this syntax so let's assign it a variable number of 88 and then let's return that so now what's interesting here is you can see that we are assigning using this syntax the other thing you notice is that the yaw or the assembly block here doesn't require semicolons and that this x is actually able to reach out of the scope and read this value of x so there's sort of like a shared scope here so let's remove this contract and deploy the new version expand it out and call the function and as you can see we get 88 so this is working this is our first version of solidity contract using an assembly block written in your so let's take a look at a different type let's let's uh let's have this set in assembly that sets the value using hexadecimal so if I copy this function and what I'll do is I'll just rename this to X Type okay and what I'm going to do in this function is I'm still going to return a un256 but in the assembly block I'm going to assign X to a hexadecimal representation of a number so let's set it to let's say 0xc so 0x C is the number 12 in um in this so let's return that let's compile it and return it so let me get rid of this one deploy and call the hex type and we see we get the number 12. so what's happened here is that in assembly we have used hexadecimal when setting the variable X but in solidity it has interpreted it as a un256 and return the decimal version which is of course 12.
so now let's move on to a more interesting data type strings so let's create a function definition called string type which is an external pure function that returns a string in memory and then what we do is we'll create a definition of a string memory here and we'll call this Str and we'll set it to an empty string to start with and then we'll have an assembly block as we've had in the previous examples and we have Str and we'll set that equal to a string let's say hello and we will return that string so so far so good now we're getting a compiler error here this is because I've got the space okay now I can move that contract and deploy it and let's see what happens when we call the string type which is here we actually don't get any output and we actually saw something in the logs here which is an out of gas error so what's the problem well actually the reason is because we're using memory so we've defined this string as being in memory but in assembly we're dealing with pointers so the the string variable here is essentially on the memory Heap but the string variable here where we're setting it is setting a pointer on the stack so this these two things are not really compatible so what we need to do is we need to use bytes 32 instead because bytes 32 types are on the stack so if we change this to bytes 32 then we can use this and we can set the string and let's see what happens now oh we need to change the return type yes that's right we need to just return the type here that's now bytes 32 because we changed it all right that compiles and let's see what we get so if we deploy that and run and call the string type we actually get a value back so we don't get a revert we don't get out of gas but we don't get our string back either because it's about 32. and the way that we can fix this is to convert the bytes 32 back into a string when we before before we before we return it here so the way to do that is you use the string type and we use ABI in code and we API encode the string variable and pass that into the string type here so we're casting it to a string and then we just have to change the return type again to a string memory and then it will compile without error and we can deploy and test this function again and this time when we call the string type we get the word back that we expect hello so what's happening here is we're dealing with bytes 32 so that we can set the pointer to the correct value as expected and then converting it back or casting it back to a string so that we can return it as a string now one thing to bear in mind is that we can only handle 32 bytes so if I create a string that's larger than 32 bytes maybe something like this we immediately get an error here saying that the string literal is too too long so basically anything longer than 32 bytes is not supported in assembly like this because we're dealing with bytes 32. so the way around this can will be discussed later on but uh for now this is how we can deal with small strings which is via converting back to a string using API encode right let's deal with another type this time let's use a Boolean for example and in this case we can see quite clearly how solidity will convert the bytes 32 that is set in the assembly code back to something that is a specific type in solidity so let's create the function definition again so a function we call this bull type and this is an external pure function that returns a ball and we're going to use solidity so what we're going to do is start off by as we've done before declaring the variable in solidity first so this would be a Boolean and then in assembly block we'll set the Boolean in assembly we need to set the Boolean to what we consider so anything we like but we're going to let's just set it to one for now and see what happens if we return it so we can come back over here and we can deploy the contract expand it out and click on board type now the interesting thing here that you'll notice it returns true even though we only set one in the assembly code this is because in solidity a Boolean is represented as a 32 byte word with the last bit set to one um well more specifically the Boolean is true when the last bit is one and it's false when the last bit is zero so here essentially what we're doing when I change this to zero is changing the our last bit to to a zero so if I deploy this and check the Boolean type again this time it's Force but let's try one more type let's try something interesting again let's use uh this same function just copy it and this time I'm going to try out address so like so address type call it a and let's set it as uh actually a 80d add R like that okay is our variable name let's set it to one and let's make sure that we return that so what are we going to get well what happens here is again this is a 32 byte word in in assembly essentially and what we're doing is we're just sending the last bit to one and so what we end up with is that it will parse this and solidity will return us a valid solidity address with the last bit as one so if we remove that and deploy again and this time we can call the address function we get an address back with the last number set to one I'd like to now move on to some examples where we can see certain operations being performed in a yule assembly block so in this example here I've got a function that returns whether or not a parameter that's passed in let's say this parameter here n is actually a prime number and the work is being done in this assembly block and the cool thing about this example is we get to see quite a few things with your a good example here of using arithmetic functions such as ADD and div there's also a mod for modulus there's a for Loop example and an if statement as well as setting variables and checking less than and is zero so what's really going on here well as we know we got this contract which is written in solidity and we pass in the variable n and we set this variable here called is prime to true now note it's defined up here in the returns statement so we don't have to declare that it's a Boolean it's already declared up here now I set the default to true and what we do is we basically say well if the number that's passed in can at any point be divided by any other number then it will return or set the variable to be false and here of course as you know we have to set that to zero since inside assembly we're dealing with bytes 32. so what we do here is we're basically just halving the value that we pass in and then we have this for Loop which goes from 2 up to the Limit here which is less than half of the value and we keep it iterating and incrementing I and for each iteration we check is the modulus of this zero meaning can we divide by this number I and if we can then that means it's not a prime so we set this is prime to force as I said here it's a zero and then we break uh now there are a few things that we can note about this one thing is that these statements here can be placed outside of the block so for example this let I equals 2 can also be placed outside here before so that's still valid and we can also place this part here if we want to be outside or within the actual Loop itself and as you can see it still compiles but we still need to keep these statements here these um braces that need to be there as sort of placeholders so this is one example of using different functions of different operations in in your so let's uh let's deploy this and just check that it actually works so if we deploy the contract and expand it out we've got this function here is prime so if we say is 2 a prime number then it returns true if 15 is 15 a prime number no it returns false how about 101 it's 101 a prime number yes it's true so this function is a good example for getting started with certain operations in your now by the way if you want to see more or have a full documentation on on the your functions you can head over to the solidity docs and there is a section in the solidity docs for your instruction set and as you can see here if we scroll down we can see some of the functions that we've been using as well as some other functions like sub and not and we've got a standard grader then and equal and so on and so on and this is the is zero helper so hopefully this is a good way for you to get started with writing operations in your in Yule to check if something is true or false is different from how it would be done in solidity this is because as mentioned your only has one data type which is a 32 byte word and so the only way we can really check for truthiness and forcing us is to check the last bit in a 32-bit word and the bit would be set to zero if it is 4C and it would be set to another any other number for it being truthy so here we have an example of a truthy statement in this case we have a value which is a result of uint 256 which we set to 88 then in this assembly block I've said if 99 which of course is basically just always going to resolve to true in this block here in assembly then the result will be set to 99. so if we run this if we deploy and call the function then we will see that check truthy returns 99. now conversely if we want to check for forciness I can copy this function here and paste it down here and in this case what we can do is we can check for forciness so in full C and in this case what we'll do is we'll say well we'll keep the result to be 88 but here we're going to say if 0 then the result is 99 but it will remain as 88 because this resolves to false so therefore this block here will not get executed and it will remain with the value set to 88. so let's give that a try we'll deploy this contract so I'll just remove that one first deploy it expand it out and now if we hit 4C we can see the return value is 88 as expected in this example function I'm making use of the is zero helper in assembly and what we're doing here is we're just checking that is zero will return true when the value passed into that function is actually zero and this is a way that we can perform negation of values in assembly using your so here in this example we've got the result set to one but because this statement here inside the assembly block will resolve to true because 0 is 0 this actually will return a bytes 32 word with the last bit set to 1. this means that this is treated as a True Result which will then set this to 2 and therefore this will return true so if we take a look at that we can deploy the contracts and we can run the function to check negation and we get 2 as expected now in this example I want to just cover quickly how you would not use not for negation now why is this well in this example you can see that not zero actually would be fine this would be resulting in a truthy statement and then we would get this set so the result starts at one this comes in here not zero results in true because not zero typically would be one right so we know that is going to be the case and so therefore it would come back as true and set the result to true so if we have a look at that we can take a look at the negation we can see here the result is as expected it's true now what does not this not statement actually do in assembly well I've written another function here that just Returns the output of not zero and really what this will do is it flips all the bits and sets everything to the highest possible value which is f in hexadecimal and this is basically the the not of zero so zero is is zero X and then zero zero zero zero but the knot of that is zero xff right so if we take a look at that I've already deployed this not zero you can see returns this so therefore given that that fact if we now copy this function and we paste it down here and let's say we want to do negation with two so we're going to say not 2 well this will still be truthy and it's not behaving as we would expect so because this is truthy the result will still be set to 2 which is unexpected so if we look at this and we look at the negation two we can see the result is still returning as true so in other words what we should be using here is the is zero helper instead so let me just return that and deploy and run the function again and now we get one as the result because is zero two is false so this never gets wrong so the moral of this story is don't use not when you want to check for negation and checking for zero always use this is zero helper the last thing I'd like to cover in this video is the fact that there is no ill statement available in your so in this example function here I've declared to get the maximum between two variables X and Y and as you can see in the assembly block we are just checking if x is greater than y so we use the GT helper function and if x is greater than y we set max to X and if Y is greater than x we set max to Y by the way this Max variable is defined over here in the return statement of the actual civil liberty function definition so if we want we can call the function over here I've already done it 101 and 88 returns 101 if we have 56 and 44 then we get 56. so this function works but the point is is that there is no else statement in your now one of the things that you might find useful is again in the documentation relating to comparisons and other functions let me just hop over to that again and just show you so we've got things like and an or an xor all of these things work in the same way as you'd expect from solidity and perhaps other C like languages so like and is a bit wise and of X and Y and or as a bitwise or of X and Y and so on and so on so I encourage you to take a look at this documentation in order to familiarize yourself with the various functions that are available in your now I will end the screencast here so that you can take some time to experiment with your in your own time and use remix as I have and I will continue this series in another screencast we'll start to dive deeper into the functionality of your so until then enjoy your programming experience and I'll see you next time
Up Next

Understanding DAOs: Decentralized Autonomous Organizations Explained
@SimplilearnOfficial
52.4K views•2022-03-23

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Operational Security Essentials: A Guide for Hacktivists (OPSEC)
@hitbsecconf
157.4K views•2012-11-26

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto



























![What is blockchain and smart contract auditing [Arabic]](https://i.ytimg.com/vi/dBLvFbpS3bk/maxresdefault.jpg)










