> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/reverse-engineering/asm1.md).

# asm1

### Challenge Description

We are given the assembly code of a function `asm1` and asked:

> What does `asm1(0x27)` return? Submit the answer in hexadecimal format (starting with `0x`).

***

### Step 1: Understanding the Assembly

Relevant assembly:

```
<+7>:  cmp DWORD PTR [ebp+0x8], 0x4ee
<+14>: jg  <asm1+38>

<+16>: cmp DWORD PTR [ebp+0x8], 0x27
<+20>: jne <asm1+30>

<+22>: mov eax, [ebp+0x8]
<+25>: add eax, 0x12
<+28>: jmp <asm1+61>

<+30>: mov eax, [ebp+0x8]
<+33>: sub eax, 0x12
```

<https://www.codeconvert.ai/assembly-to-c-converter?id=6eccffaf-6eac-42e0-95e8-fa924cedf0a2>

<img src="https://github.com/user-attachments/assets/64f61f3d-655e-4f20-b36a-6387b1e8338a" alt="image" height="355" width="1034">

This translates to the following C-like logic:

```c
int asm1(int x) {
    if (x > 0x4ee) {
        if (x == 0x7b8) {
            return x - 0x12;
        } else {
            return x + 0x12;
        }
    } else {
        if (x == 0x27) {
            return x + 0x12;
        } else {
            return x - 0x12;
        }
    }
}
```

***

### Step 2: Evaluate the Input

We are given:

```
x = 0x27
```

#### Check first condition:

```
x > 0x4ee
0x27 > 0x4ee → false
```

So execution moves to the `else` block.

***

### Step 3: Evaluate Inner Condition

```
if (x == 0x27)
```

This is true, since:

```
x = 0x27
```

So we execute:

```
return x + 0x12
```

***

### Step 4: Compute the Result

```
0x27 + 0x12
```

Convert to decimal:

```
0x27 = 39
0x12 = 18
```

Add:

```
39 + 18 = 57
```

Convert back to hex:

```
57 = 0x39
```

***

### Final Answer

```
0x39
```

***

### Key Takeaways

* Always follow the control flow using `cmp` and conditional jumps (`jg`, `jne`).
* Map assembly to simple C logic for clarity.
* Evaluate conditions step-by-step with the given input.
* Keep everything consistent in hexadecimal when required.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/picoctf-writeups/picoctf/reverse-engineering/asm1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
