Assembly Language for Beginners

(nextflipdebug2) #1

8.6. “QR9”: RUBIK’S CUBE INSPIRED AMATEUR CRYPTO-ALGORITHM


.text:00541067 get_bit endp


...in other words: calculate an index in the cube64 array:arg_4 + arg_0 * 8. Then shift a byte from the
array by arg_8 bits right. Isolate the lowest bit and return it.


Let’s see another function,set_bit():


.text:00541000 set_bit proc near
.text:00541000
.text:00541000 arg_0 = dword ptr 4
.text:00541000 arg_4 = dword ptr 8
.text:00541000 arg_8 = dword ptr 0Ch
.text:00541000 arg_C = byte ptr 10h
.text:00541000
.text:00541000 mov al, [esp+arg_C]
.text:00541004 mov ecx, [esp+arg_8]
.text:00541008 push esi
.text:00541009 mov esi, [esp+4+arg_0]
.text:0054100D test al, al
.text:0054100F mov eax, [esp+4+arg_4]
.text:00541013 mov dl, 1
.text:00541015 jz short loc_54102B


The value in theDLis 1 here. It gets shifted left by arg_8. For example, if arg_8 is 4, the value in theDL
register is to be 0x10 or 1000b in binary form.


.text:00541017 shl dl, cl
.text:00541019 mov cl, cube64[eax+esi*8]


Get a bit from array and explicitly set it.


.text:00541020 or cl, dl


Store it back:


.text:00541022 mov cube64[eax+esi*8], cl
.text:00541029 pop esi
.text:0054102A retn
.text:0054102B
.text:0054102B loc_54102B:
.text:0054102B shl dl, cl


If arg_C is not zero...


.text:0054102D mov cl, cube64[eax+esi*8]


...invert DL. For example, if DL’s state after the shift is 0x10 or 0b1000, there is 0xEF to be after theNOT
instruction (or 0b11101111b).


.text:00541034 not dl


This instruction clears the bit, in other words, it saves all bits inCLwhich are also set inDLexcept those in
DLwhich are cleared. This implies that ifDLis 11101111b in binary form, all bits are to be saved except
the 5th (counting from lowest bit).


.text:00541036 and cl, dl


Store it back:


.text:00541038 mov cube64[eax+esi*8], cl
.text:0054103F pop esi
.text:00541040 retn
.text:00541040 set_bit endp


It is almost the same asget_bit(), except, if arg_C is zero, the function clears the specific bit in the array,
or sets it otherwise.


We also know that the array’s size is 64. The first two arguments both in theset_bit()andget_bit()
functions could be seen as 2D coordinates. Then the array is to be an 8*8 matrix.


Here is a C representation of what we know up to now:

Free download pdf