# 3.62 Switch I

The code that follows shows an example of branching on an enumerated type value in a switch statement. Recall that enumerated types in C are simple a way to introduce a set of names having associated integer values. By default, the values assigned to the names count from zero upward. In our code, the actions associated with the different case labels have been omitted.

```c
/* Enumerated type creates set of constants numbered 0 and upward */
typedef enum {MODE_A, MODE_B, MODE_C, MODE_D, MODE_E} mode_t;
long switch3(long *p1, long *p2, mode_t action) {
    long result = 0;
    switch(action) {
        case MODE_A:
            result = *p2;
            *p2 = *p1;
            break;
        case MODE_B:
            result = *p1 + *p2;
            result = *p1;
            break;
        case MODE_C:
            *p1 = 59;
            result = *p2;
            break;
        case MODE_D:
            *p1 = *p2;
            result = 27;
            break;
        case MODE_E:
            result =27;
            break;
        default:
            result = 12;
            break;
    }
    return result;
}
```

The part of the generated assembly code implementing the different actions is shown below. The annotations indicate the argument locations, the register values, and the case labels for the different jump destinations.

Fill in the missing parts of the C code. It contained one case that fell through to another--try to reconstruct this.

Assembly code:

```c
; p1 in %rdi, p2 in %rsi, action in %edx
.L8: ; MODE_E
	movl 	$27, %eax
	ret
.L3: ; MODE_A
	movq 	(%rsi), %rax
	movq 	(%rdi), %rdx
	movq 	%rdx, (%rsi)
	ret
.L5: ; MODE_B
	movq 	(%rdi), %rax
	addq 	(%rsi), %rax
	movq 	%rax, (%rdi)
	ret
.L6: ; MODE_C
	movq 	$59, (%rdi)
	movq 	(%rsi), %rax
	ret
.L7: ; MODE_D
	movq 	(%rsi), %rax
	movq 	%rax, (%rdi)
	movl 	$27, %eax
	ret
.L9: ; default
	movl 	$12, %eax
	ret
```


---

# Agent Instructions: 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:

```
GET https://valineliu.gitbook.io/deuterium-wiki/reading/cs-jing-dian-shu-ji/csapp-3e-homework-solution/3.-machine-level-representation-of-programs/3.62-switch-i.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
