I\'m stumped. I need help to translate the following C code (the for loop; not w
ID: 3911980 • Letter: I
Question
I'm stumped. I need help to translate the following C code (the for loop; not what's commented out) into assembly language.
Goal: Working program that accesses the input file data and applies a simple encryption to that data. The encryption program must loop through the input file and xor every byte with keyfile [starting index].
Note4: starting_index = gPasswordHash[0] * 256 + gPasswordHash[1];
for ( x = 0; x < fileLength(input_file); x++) {
file[x] = file[x] ^ gKey[index];
index += hop_count[round];
if(index ? 65537) index -= 65537;
// for each file[x]:
// (#A) rotate 3 bits to left 0xA5 0x2D
// (#B) swap nibbles 0x2D 0xD2
// (#C) reverse bit order 0xD2 0x4B
// (#D) swap even/odd bits 0x4B 0x87
// (#E) code table swap 0x87 CodeTable[0x87]
} // end for loop through file
This is what I came up with so far...
mov x = 0
loop2:
mov eax, x
cmp eax, fileLength(input_file)
jl endloop2
mov ebx, file[x]
not sure if that's even right
Explanation / Answer
SOLUTION:
This solution having explanation , assembly code equivalent to given
c code ,comments with code for clear and better understanding.
EXPLANATION:
Before going to direct answer let's see some function for more understanding .
MOV a,b : move value of b into a
CMP a,b : compare value of a and b , whether a > or < or = to b
JG c: jump to c if greater
XOR a,b : perform XOR operation of a and b and store result into a
ADD a,b : add a and b and store result into a .
Now let's see code equivalent to given uncommented c code :
assembly code :
# initialise x with zero
MOV x,0
# loop untill x<fileLength(input_file)
loop2:
# mov value of x into eax register
MOV eax,x
# compare for loop
CMP eax,fileLength(input_file)
# jump to endloop2 if greater
JG endloop2
MOV ebx,file[x]
MOV ecx,gkey[index]
# perform XOR
XOR ebx,ecx
MOV file[x],ebx
# addition
ADD index,hop_count[round]
endloop2:
since everyting is provided for better and clear understanding
However further if any difficulty in understanding the code then feel
free to ask .
i will help you.