/* Like this: struct chicken { char breed[40]; int count = 0; }; */ .equ breed, 0 .equ count, 40 .equ chicken_size, 44 .data chicken_prompt: .asciz "Enter a chicken: " scanf_format: .asciz "%s" print_format: .asciz "%s : %d\n" .text record_chicken: push $chicken_prompt call puts add $4, %esp sub $40, %esp push %esp push $scanf_format call scanf add $8, %esp mov $0, %esi for_each_chicken: /* Compare the string at %esp with the one at chickens[%esi] */ push %esp mov %esi, %eax mov $44, %ebx mul %ebx add %ebp, %eax mov %eax, %edi push %eax call strcmp /* Fix stack from strcmp */ add $8, %esp cmp $0, %eax je inc_chicken inc %esi cmpb $0, (%edi) jne for_each_chicken // Copy chicken name from %esp to chickens[%esi] /* Re-use parameters for strcmp */ sub $8, %esp call strcpy add $8, %esp add $40, %edi movl $1, (%edi) jmp fix_stack_and_return inc_chicken: mov %esi, %eax mov $44, %ebx mul %ebx add %ebp, %eax add $40, %eax movl (%eax), %ebx inc %ebx movl %ebx, (%eax) fix_stack_and_return: add $40, %esp ret print_chickens: mov $0, %esi print_chicken_loop: // Still calculate index of current chicken in eax mov %esi, %eax mov $44, %ebx mul %ebx add %ebp, %eax cmpb $0, (%eax) je done add $40, %eax push (%eax) sub $40, %eax push %eax push $print_format call printf add $12, %esp /* Increment this */ inc %esi jmp print_chicken_loop done: ret .global _start _start: // Array of chickens // Like struct chicken chickens[10]; sub $440, %esp mov %esp, %ebp push $440 push $0 push %ebp call memset add $12, %esp // Use ctrl+c to exit main_loop_top: call record_chicken call print_chickens jmp main_loop_top