X86 assembly – Handling the IDIV instruction

The first part of Mysticials answer is correct, idiv does a 128/64 bit division, so the value of rdx, which holds the upper 64 bit from the dividend must not contain a random value. But a zero extension is the wrong way to go. As you have signed variables, you need to sign extend rax to rdx:rax. There is a specific instruction for this, cqto (convert quad … Read more

JNZ & CMP Assembly Instructions

JNZ is short for “Jump if not zero (ZF = 0)”, and NOT “Jump if the ZF is set”. If it’s any easier to remember, consider that JNZ and JNE (jump if not equal) are equivalent. Therefore, when you’re doing cmp al, 47 and the content of AL is equal to 47, the ZF is set, ergo the jump (if Not … Read more

writing functions in assembler

(assuming NASM x86) Use call in order to call the function and ret to return from the function. What occurs when you type call is that the address of the next instruction is pushed into the stack. When ret is hit, it will pop that address off the stack and jmp to it. Calling convention dictates that the EAX register should contain the return value. Also note that the __cdecl calling … Read more

Assembly – JG/JNLE/JL/JNGE after CMP

When you do a cmp a,b, the flags are set as if you had calculated a – b. Then the jmp-type instructions check those flags to see if the jump should be made. In other words, the first block of code you have (with my comments added): would jump to label1 if and only if al was greater than dl. You’re probably better … Read more

How do AX, AH, AL map onto EAX?

No, that’s not quite right. So AX is composed of AH:AL halves, and is itself the low half of EAX. (The upper half of EAX isn’t directly accessible as a 16-bit register; you can shift or rotate EAX if you want to get at it.) For completeness, in addition to the above, which was based … Read more

How can one see content of stack with GDB?

info frame to show the stack frame info To read the memory at given addresses you should take a look at x x/x $esp for hex x/d $esp for signed x/u $esp for unsigned etc. x uses the format syntax, you could also take a look at the current instruction via x/i $eip etc.

MIPS program jr $ra instructions and stack handling

On function entry, ra holds the return address where our caller wants us to jump when we’re done. The function preamble saves it to the stack because the function body uses jal to make function calls. jal overwrites ra so we need to save/restore our own return address around that. When the function is complete we can restore the things we saved, then … Read more