RE: [Tutorial Request] Assembly Programming 09-18-2012, 04:41 PM
#7
That's x86 assembly.
INT 21 basically raises software interrupt, but that doesn't do anything by itself (except raising the software interrupt and running the interrupt handler code). What happens specifically depends on the environment and how is the processor configured and what code is actually placed at the respective interrupt vector.
The way you describe its use is tied to a specific operating system (DOS environment for example) where interrupts are used by user-space programs for communication with the operating system.
Basically when the operating system boots up, it setups the software interrupts and prepares a pieces of code at the interrupt vectors (memory locations, which will the processor jump to when an interrupt is raised). When the code of your program is running, the OS code is basically sleeping (a single core processor can run only one portion of code at once), the CPU is focused on your own code. But when it gets to the INT instruction, it will raise an interrupt.
Interrupt basically... well it interrupts whatever the processor is doing at the moment (which is your program), saves its state, so it can resume the work later and jumps to the interrupt vector, where the OS already prepared code to handle the interrupt vector. This code will basically do the given task, like getting input from the user and once it's done with that, it will tell the CPU that the interrupt routine is done. The CPU will load the state of execution of your program and will resume work on your code. The OS however somehow prepared the information for your program, for example it stored an address of the user input string in some pre-determined register, from where your program can read it and do something with it.
However, if there was no OS, INT 21 wouldn't do that, it wouldn't get the string from user. That's a complicated task and the processor cannot do that. The OS however provides a code that achieves this complicated task (using series of instructions) and makes it available to other programs, so they don't have to redefine this operation themselves (and sometimes they can't even do it themselves because they don't have necessary rights, so the OS can verify if the data you're sending it are okay, to prevent any damage or security breach).
-------------------------------------
Anyway, SHL and SHR basically shift the binary data left or right (SHift Left and SHift Right) that's their primary function and fast multiplication and division is one of the uses, but not the primary use. Also because the division/multiplication can be only in multiples of two.
You can use SHL and SHR for example to pack two 8-bit values into a single 16-bit value for example or do various bit testing with masks and such. There are many uses, it always depends on what you're doing.
ROL, RCL, ROR and RCR are similar, except that they do rotation, not shifting, meaning that when you rotate right, then the right-most bit will become left-most, with shifting it's basically lost.
The difference between ROL and RCL (both rotate left) is, that RCL does the rotation via the carry bit. So when you rotate left, then the left-most bit goes to the carry bit (which is stored in another register), so you can use it with conditional jumps for example. Let's say you have some value where individual bits determine some operations that need to be done, so you keep rotating it by one place and testing the carry bit and preforming some operation based on that and after you do a full turn with the rotations, the data that you rotated will be restored to original un-rotated form.
It's impossible to say generally what are they used for, there are many uses and it depends on the context and what you're doing, often there are multiple ways to do it, for example you can even do equivalent of ROL with SHL, AND and OR and some registers. Same with SHL, you can express the same operation with ROL and some bitwise instructions.
I recommend checking the documentation for exact information about what do these instructions specifically do, which registers and bits they affect, what parameters they can be used with and such.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.