Where and when do we use the keywords 'asm' & 'short' in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Where and when do we use the keywords 'asm' & 'short' in c++?

please give me the example where we use these keywords -» asm & short in c++?

20th Mar 2018, 3:27 AM
Suraj Jha
Suraj Jha - avatar
4 Answers
+ 3
Short can be used any place you might use int, when the integer range is within 2 bytes or -32768 to 32767. Asm allows you to code Assembly instructions. asm("ret"); // Return from the function http://en.cppreference.com/w/cpp/language/asm
20th Mar 2018, 4:20 AM
John Wells
John Wells - avatar
+ 3
Is asm part of the standard or is it a compiler feature?
20th Mar 2018, 5:16 AM
Timon Paßlick
+ 3
Officially, it is conditionally-supported and implementation defined. GCC supports it so it works in SoloLearn playground. There are a bunch of codes I've seen here. As to running with other compilers, there is no guarantee.
20th Mar 2018, 5:23 AM
John Wells
John Wells - avatar
+ 1
The 'asm' keyword is for inline assembly code, I believe, and 'short' is for integral type variables. It is "an integral type that is larger than or equal to the size of type char, and shorter than or equal to the size of type int" (https://msdn.microsoft.com/en-us/library/cc953fe1.aspx). I don't know of any examples for asm, as I've never written in assembly; however, using short is just as easy as using long. Say I have a player, and I use a variable called 'level' that keeps their level. I don't ever need the level to go very high. Maybe 500 is the highest it will go, so... short int level = 1; //the limit of short int is +/- 32,767
20th Mar 2018, 4:23 AM
Zeke Williams
Zeke Williams - avatar