//----------------------------------------------------------------- // iseven.s // // Here is one possible answer to the programming question on // our Midterm Exam I (Question V). This program will accept // an integer entered by the user as a command-line argument, // and it will determine whether or not that integer is even. // // to assemble: $ as iseven.s -o iseven.o // and to link: $ ld iseven.o base10io.o -o iseven // // programmer: ALLAN CRUSE // written on: 25 FEB 2007 //----------------------------------------------------------------- # manifest constants .equ sys_EXIT, 1 .equ sys_WRITE, 4 .equ dev_STDOUT, 1 .section .data arg: .int 0 # for program's argument rem: .int 0 # for division remainder len: .int 0 # for argstring's length msg0: .ascii " is an even number.\n" # message's text len0: .int . - msg0 # message's size msg1: .ascii " is not an even number.\n" # message's text len1: .int . - msg1 # message's size .section .text _start: # quit if no command-line argument was entered cmpl $1, (%esp) # only one command-string? je exit # if yes, quit immediately # else convert the asciz-string into an integer mov 8(%esp), %esi # string-address into ESI call asc2eax # call 'asc2int' function mov %eax, arg # save the integer value # next determine the asciz-string's length movl $0, len # initialize byte-count mov 8(%esp), %esi # point ESI to argstring nxcmp: cmpb $0, (%esi) # final null-byte found? je cmpxx # yes, finished counting incl len # else add one to length incl %esi # advance string-pointer jmp nxcmp # and compare next byte cmpxx: # then write the argument-string to the screen mov $sys_WRITE, %eax # system-call ID-number mov $dev_STDOUT, %ebx # device-file ID-number mov 8(%esp), %ecx # address of the string mov len, %edx # length of the string int $0x80 # invoke kernel service # use division-by-two to check the argument's parity mov arg, %eax # setup arg as dividend xor %edx, %edx # extend it to quadword mov $2, %ecx # setup divisor in ECX div %ecx # perform the division mov %edx, rem # store the remainder # use compare-and-branch to show the proper message cmpl $0, rem # remainder was zero? jne isodd # no, number not even # show message #0 mov $sys_WRITE, %eax # system-call ID-number mov $dev_STDOUT, %ebx # device-file ID-number lea msg0, %ecx # address of message mov len0, %edx # length of message int $0x80 # invoke kernel service jmp exit # then quit the program isodd: # show message #1 mov $sys_WRITE, %eax # system-call ID-number mov $dev_STDOUT, %ebx # device-file ID-number lea msg1, %ecx # address of message mov len1, %edx # length of message int $0x80 # invoke kernel service jmp exit # then quit the program exit: # terminate this program mov $1, %eax # system-call ID-number int $0x80 # invoke kernel service .global _start # make entry-point public .end # no more to be assembled