//---------------------------------------------------------------- // idnumber.s (A solution to Question V on Final Exam) // // This program will use the 'getpid' system-call to obtain // its own PID (Process Identification Number), and then it // will display that 16-bit integer using base 10 notation. // // assemble using: $ as idnumber.s -o idnumber.o // and link using: $ ld idnumber.o -o idnumber // // programmer: ALLAN CRUSE // written on: 21 MAY 2006 //---------------------------------------------------------------- # manifest constants .equ sys_getpid, 20 # ID-number for 'getpid' .equ sys_write, 4 # ID-number for 'write' .equ sys_exit, 1 # ID-number for 'exit' .equ STDOUT, 1 # ID-number for output .section .data pid: .short 0 # stores a 16-bit integer msg: .ascii "\n\t\tpid = " # message to report PID val: .ascii " \n\n" # buffer for digit-string len: .int . - msg # length of message text .section .text _start: # obtain this task's pid mov $sys_getpid, %eax # system-call ID-number int $0x80 # enter Linux kernel mov %ax, pid # save this task's PID # convert pid from binary to decimal digit-string mov pid, %ax # setup PID as dividend mov $10, %bx # setup radix as divisor lea val, %edi # setup buffer's address xor %ecx, %ecx # initialize digit-count nxdiv: xor %dx, %dx # dividend zero-extended div %bx # divide by decimal base push %edx # save remainder on stack inc %ecx # increment digit-counter or %ax, %ax # is the quotient nonzero? jnz nxdiv # yes, get another digit nxdgt: pop %edx # recover prior remainder add $'0', %dx # convert number to ascii mov %dl, (%edi) # store numeral in buffer inc %edi # advance buffer pointer loop nxdgt # again for other digits # display the message mov $sys_write, %eax # system-call ID-number mov $STDOUT, %ebx # device-file ID-number lea msg, %ecx # address of the string mov len, %edx # length of the message int $0x80 # enter Linux kernel # terminate this program mov $sys_exit, %eax # system-call ID-number mov $0, %ebx # value for 'exit-code' int $0x80 # enter Linux kernel .global _start # makes entry-point visible .end # no more lines to assemble