GAS LISTING ba.s page 1 1 .file "ba.s" 2 .globl buff0 3 .data 4 .align 4 5 .comm num0,4,4 6 .comm num1,4,4 7 .comm num2,4,4 8 .comm total,4,4 9 buff0: 10 0000 20202020 .ascii " ?" 10 203F 11 0006 20706C75 .ascii " plus " 11 7320 12 buff1: 13 000c 3F .ascii "?" 14 000d 20706C75 .ascii " plus " 14 7320 15 buff2: 16 0013 3F .ascii "?" 17 0014 20657175 .ascii " equals " 17 616C7320 18 buff3: 19 001c 20202020 .ascii " ?" 19 3F 20 0021 0A00 .string "\n" 21 0023 00000000 .zero 6 21 0000 22 .section .rodata 23 .text 24 .align 4 25 .globl main 26 .type main,@function 27 main: 28 0000 55 pushl %ebp # Save the old frame pointer 29 0001 89E5 movl %esp, %ebp # Start a new fram here 30 0003 6A22 pushl $34 # Setup buffer size 31 0005 68000000 pushl $buff0 # buffer start address and 31 00 32 000a 6A01 pushl $1 # file descriptor for standard out 33 000c E8FCFFFF call write # and use low-level write service 33 FF 34 0011 83C40C addl $12, %esp # Reset stack pointer 35 # movl 12(%ebp), %ecx # Caused a segmentation error 36 0014 8B450C movl 12(%ebp), %eax # Get the pointer to the start of argv 37 0017 8B7004 movl 4(%eax),%esi # Get the ptr to the second argument 38 001a 31C0 xorl %eax,%eax # Begin with a 0 value 39 nextdigit: 40 001c 0FBE0E movsbl (%esi), %ecx # Get the ASCII number 41 001f 80F930 cmpb $'0', %ecx # Is it less than an ASCII 0? 42 0022 7C14 jl numend # Yes, assume end of ASCII digits 43 0024 80F939 cmpb $'9', %ecx # No, is it greater than a '9'? 44 0027 7F0F jg numend # Yes, probably garbage 45 0029 83E930 subl $'0', %ecx # Make the new digit binary 46 002c BA0A0000 movl $10, %edx # and move the 46 00 47 0031 F7E2 mul %edx # old number over one digit 48 0033 01C8 addl %ecx, %eax # Add the new low order digit to it 49 0035 46 inc %esi # Adjust pointer to next possible digit 50 0036 EBE4 jmp nextdigit # and go look 51 numend: 52 0038 A3000000 movl %eax, num0 # Save the binary 1st number 52 00 53 003d C7050000 movl $1, num1 # Save the binary 2nd number 53 00000100 53 0000 54 0047 C7050000 movl $2, num2 # Save the binary 3rd number 54 00000200 54 0000 55 0051 A1000000 movl num0, %eax # Fetch the first binary number 55 00 56 0056 03050000 addl num1, %eax # Add the second binary number 56 0000 57 005c 03050000 addl num2, %eax # Add the third binary number 57 0000 58 0062 A3000000 movl %eax, total # Save result 58 00 59 60 0067 8D3D2000 leal buff3+4, %edi # Point to least significant position 60 0000 61 006d B90A0000 mov $10, %ecx # Store 10s digit divisor 61 00 62 nxtpos: 63 0072 31D2 xor %edx, %edx # Clear high order dividend register 64 0074 F7F1 div %ecx # Strip off the least significant digit 65 # add %edx, '0' # Caused a segmentation error 66 0076 83C230 add $'0', %edx # Convert the digit to ASCII 67 0079 8817 movb %dl, (%edi) # Put next digit into the buffer 68 007b 4F decl %edi # Move over to the next digit position 69 007c 21C0 andl %eax,%eax # Has the remainder reached zero? 70 007e 75F2 jnz nxtpos # No, do the next most significant digit 71 72 0080 0FBE0500 movsbl num1, %eax # Fetch the second binary number 72 000000 73 0087 0430 addb $'0', %al # Make second number ASCII 74 0089 88050C00 movb %al, buff1 # Save second number in buffer 74 0000 75 008f 0FBE0500 movsbl num2, %eax # Fetch the 3rd binary number 75 000000 76 0096 0430 addb $'0', %al # Make 3rd number ASCII 77 0098 88051300 movb %al, buff2 # Save 3rd number in buffer 77 0000 78 009e 6A22 pushl $34 # Do 79 00a0 68000000 pushl $buff0 # write 79 00 80 00a5 6A01 pushl $1 # system 81 00a7 E8FCFFFF call write # service 81 FF 82 00ac 83C40C addl $12, %esp # and reset stack pointer 83 .L1: 84 00af C9 leave 85 00b0 C3 ret DEFINED SYMBOLS *ABS*:00000000 ba.s ba.s:9 .data:00000000 buff0 *COM*:00000004 num0 *COM*:00000004 num1 *COM*:00000004 num2 *COM*:00000004 total ba.s:12 .data:0000000c buff1 ba.s:15 .data:00000013 buff2 ba.s:18 .data:0000001c buff3 ba.s:27 .text:00000000 main ba.s:39 .text:0000001c nextdigit ba.s:51 .text:00000038 numend ba.s:62 .text:00000072 nxtpos UNDEFINED SYMBOLS write