# # File: switch1_mod.s # Author: William T Krieger, Oct 2003 # # My modification of the switch1 test case demonstrating a # simple switch statement. .text .align 32 LC0: .ascii "Enter 1. Add, 2. Sub, 3. Mul, 4. Div: \0" LC1: .ascii "%d\0" FMT_ADD: .ascii "Add!\12\0" FMT_SUB: .ascii "Sub!\12\0" FMT_MUL: .ascii "Mul\12\0" FMT_DIV: .ascii "Div\12\0" FMT_BAD: .ascii "Bad choice\12\0" FMT_DONE: .ascii "Done.\12\0" .align 4 .globl _main _main: pushl %ebp # enter main() movl %esp,%ebp pushl $LC0 # call printf( fmt) call _printf addl $4,%esp addl $-4,%esp # adjust stack for 1 local var: int x; leal -4(%ebp),%eax # call scanf( fmt, &x) pushl %eax pushl $LC1 call _scanf movl -4(%ebp),%eax # eax = x cmpl $1,%eax # if x == 1, then jmp CASE_ADD je CASE_ADD cmpl $2,%eax # if x == 2, then jmp CASE_SUB je CASE_SUB cmpl $3,%eax # if x == 3, then jmp CASE_MUL je CASE_MUL cmpl $4,%eax # if x == 4, then jmp CASE_DIV je CASE_DIV ### # For each case label below, the appropriate format string is pushed # onto the stack so that printf can be called at the CASE_WRAPUP label ### pushl $FMT_BAD # default case, pushes an error message jmp CASE_WRAPUP CASE_ADD: pushl $FMT_ADD jmp CASE_WRAPUP CASE_SUB: pushl $FMT_SUB jmp CASE_WRAPUP CASE_MUL: pushl $FMT_MUL jmp CASE_WRAPUP CASE_DIV: pushl $FMT_DIV jmp CASE_WRAPUP CASE_WRAPUP: call _printf # calls printf, using one of the format strings # pushed in a case label above addl $4,%esp DONE: pushl $FMT_DONE # call printf( fmt) call _printf addl $4,%esp movl %ebp,%esp # exit main() popl %ebp ret