Search code examples
assemblyx86reverse-engineering

Reverse engineering assembly code


Am am trying to learn assembly / reversing. I am struggeling to find documentation for idiv and imul with only one argument for example, and also understand for which argument is first and last in sub/add. I have been given the following code (with my understanding in comments)

start:
mov $1024, %rax ; move int value 1024 to register rax
mov $4096, %rbx ; move int value 4096 to register rbx
mov $2048, %rcx ; move int value 2048 to rax rcx
xor %rdx, %rdx ; rcx XOR rcx. rcx = 0
sub %rcx, %rbx ; sucstract rcx from rbx? rbx = 2048?
cmp %rbx, %rax ; compare rbx to rax
jge loopa ; if rax > rbx, jump to loopa ? (false first time)
jmp loopb ; else jump to loopb
loopa: ; start loop a 
cmp $4, %rdx ; compare int value 4 to register rdx
jg end ; if rdx > 4 jump to end 
inc %rdx ; rdx++ (rdx = 1 first time)
loopb:
xchg %rax, %rbx ; (switch value of rax and rbx)
idiv %rbx ; signed divide, but divide on what? 
add %rdx, %rax ; add rdx to rax ?
imul %rcx ; (multiple what? )
jmp loopa ; jump to loopa
end:

Is this a given dialect of x86 assembly? I have searched for instructions, and i see that they differ. Both in arguments, syntax and so on.


Solution

  • probably a mistype, but I want to make you notice anyway: in this line jge loopa ; if rax > rbx, jge means Jump if Greater or Equal, so if rax >= rbx. Remember that every AT&T (yes, it's a sort of dialect of intel syntax) instruction has this pattern: instruction source, destination. So for arithmetic or comparison instruction you have to start "thinking" from the second operand.

    About idiv and imul instruction, you should read this.