You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/post/binary-bomb-phase-6.md
+35-35Lines changed: 35 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -150,146 +150,146 @@ Let's go through the assembly step-by-step and map it to the C code:
150
150
- We see a call to the function 'read_six_numbers'. Add a breakpoint on the call instruction if you want to explore the function.
151
151
- The function `read_six_numbers` will take `rcx`, `rdx` as arguments. `rdx` is set with the address of `rbp+48h`. The user input values will be set from the address starting at `rdx`, and hence `rbp+48h` will have the input values.

155
155
156
156
- Add breakpoint to check the output of read six numbers. It can be seen that even if the input contains more than 6 digits, the output is 6, so it could mean that first 6 digits would be considered.

160
160
161
161
- After the call to read_six_numbers, a variable `rbp+0c4h` (let's call this as variable `a`) is set to 0 and there is jump statement.
162
162
163
163
- The jump statement takes the control to the compare function. This function checks if the value of the variable is greater than or equal to 6.
164
164
165
-

165
+

166
166
167
167
- If the value is greater than equal to 6, the jump statement is executed and takes the control to a different location which is outside the loop. This seems like the false condition for a loop.
168
168
169
-

169
+

170
170
171
171
- But, if the value of `a` is less than 6, the jump does not take place and flow continues after the jump statement.
- If the value is more than 1, then again the `inp[a]` is being compared with 6. If the value is greater than 6, the jump statement is not executed and explode_bomb function will be called.
182
182
**So, the value of `inp[a]` should be in range of 1 and 6.**
- If the value of `a` is in [1,6], then a new variable `rbp+0e4h` (let's call it variable `b`) is assigned the value `a+1`.
187
187
- The jump statement takes to condition statement, where the value of `b` is compared with 6. If the value is greater than or equal to 6, the jump statment again takes control to the increment operation of variable `a`.
188
188
189
189
- If the value of `b` is less than 6, then there is a comparison between `inp[a]` and `inp[b]`.
- If the values are equal, the jump statement is not taken, and explode_bomb function is called. If the values are not equal, the jump statement takes the control to increment of variable `b` and back to the loop condition of comparison of value `b` with 6.
194
194
195
-

196
-

195
+

196
+

197
197
198
198
- Now, if we go back and look around the read_six_numbers function, there is an instruction related to node. Address of this node is saved to `rbp+8`.
- This shows a Linked List structure where each node has a value and reference the next node address. The last node with the address *b108f000* seems to be the last node, as it doesn't point to another address.
209
209
210
210
- When the condition of main loop became false, the just statement took the control to outside the loop where the value of `a` is set to 0 and then jump statement takes to another condition where `a` is compared with 6. This seems to be the start of another main loop.
211
211
212
-

212
+

213
213
214
214
- If the value of `a` again is greater than or equal to 6, the control passes to a different location.
215
215
216
216
- If the value of `a` is less than 6, `rbp+8` which contains the address of first linked list node, is passed to `rbp+28h`. The value of `b` is set to 1.
217
217
Let's call the node at `rbp+8` as `head`, and `rbp+28h` as `curr` (current).
- The value of `b` is compared with the value of array at index `a`. If value of `b` is less, the value of `curr` is set to `curr->next` (the next node). For each node, the value at the address is the node value, then there is index, and then the address to next node. So, the <nodeaddress> + 8, gives the address of next node.
222
222
223
-

- The value of `b` is increased by 1 and agin the comparison starts, so it means the comparison of `b` with value of array at index `a` was a loop condition (nested loop) and here the loop ends.
227
227
228
-

228
+

229
229
230
230
- If the value of `b` when compared with value of array at `a` was greater, then the jump would pass the control outside the inner loop. `rbp+78h` contained the address of the start of the node of linked list. From this address, an array is taken (let's call it `narr`), whose value at index *a*`narr[a]` is set with the value of `curr` node.
- Jump instruction takes the control to iteration where the value of `a` is incremented by 1, and again the condition is checked, which means the loop ends.
- If the value of `a` becomes greater than or equal to 6, then the loop condition breaks and control passes outside the loop.
239
239
The address at `rbp+78h` (`narr[0]` node array) is being set to `rbp+8` (`head` node). The address from `rbp+8` (`head`) is being set to `rbp+28h` (`curr` node).
240
240
241
-

241
+

242
242
243
243
- We can check the value of `rbp+78` (`narr`) has address of different nodes of the linked list.
244
244
245
-

245
+

246
246
247
247
- We can break at the line where `rbp+28h` (`curr`)is assigned and check the value. It has the starting node address of linked list.
- So, this indicates something like `head = narr[0]; curr = head;`
252
252
253
253
- The value of `a` is set to 1, and jump takes the control to a comparison statement which again compares the value of `a` with 6. This could be the start of another loop.
- If the value of `a` is greater than or equal to 6, the jump statement takes the control to a different location.
258
258
259
259
- If the value of `a` is less, the loop starts/continues.
260
260
The `curr` node (`rbp+28h`) next element is set to the node at index `a` of the node array `narr` (`curr->next = narr[a]`). Then `curr` node is again set to `curr->next`.
- The jump instruction takes the control to iteration where the value of `a` is increased by 1. After the iteration again there is the comparison statement of `a` which is the loop condition, so the loop ends.
- If the value of `a` becomes more than or equal to 6, the loop breaks and control moves out of loop.
269
269
The `curr` node next address is set to 0, which indicates to NULL. It means that the `curr` node does not point to any other node.
270
270
- Then the `curr` node is pointed to the `head` node. Value of `a` is set to 0.
271
271
272
-

272
+

273
273
274
274
- The jump statement then takes the control to the comparison instruction. If the value of `a` is greater than or equal to 5, jump statement takes the control to another location.
- If the value of `a` is less than 5, comparison happens between values of `curr` and `curr->next`. If `curr` value is less than `curr->next` value, the `explode_bomb` function is being called.
- If the value of `curr` is equal to or greater than value of `curr->next`, then `curr` node is set to `curr->next` node.
283
283
284
-

284
+

285
285
286
286
- The jump statement takes the control to iteration where the value of `a` is increased by 1. The loop condition comes again, which means that the loop ends.
0 commit comments