//----------------------------------------------------------------- // sqroots.s (A modification of our 'showreal.s' demo) // // This is a possible answer to the Final Exam's Question V. // It uses the 'fsqrt' floating-point instruction, plus code // from our 'showreal.s' demo-program, to create a loop that // prints the square-roots of the integers from 1 through 9. // // programmer: ALLAN CRUSE // written on: 23 APR 2007 // revised on: 16 MAY 2007 -- to use the 'fsqrt' instruction //----------------------------------------------------------------- .section .data x: .int 0 # to store current integer real: .float 0.0 # to hold each square-root temp: .int 0 # storage for its multiple tento4: .int 10000 # decimal-point will shift msg: .ascii " The square-root of " # beginning of the message num: .ascii "x is " # continuation of message buf: .ascii " \n" # conclusion of message len: .int . - msg # length for output string .section .text _start: movl $1, x # initialize variable 'x' again: # put the numeral for 'x' into 'num' mov x, %eax # fetch current x-value add $'0', %al # convert to ascii-code mov %al, num # and insert in message # compute the square-root of 'x' finit # initialize coprocessor fild x # get the current x-value fsqrt # compute its square-root fstp real # store result as 'float' # multiply the real-number by 10**4 finit # initializes coprocessor fld real # load the real number fild tento4 # load multiplier-factor fmulp # multiply and pop st(0) fistp temp # store st(0) as integer fwait # synchronize processors # convert the 'temp' integer to a decimal digit-string mov temp, %eax # temp into accumulator mov $10, %ebx # and radix into EDX mov $5, %ecx # setup digit counter nxdiv: xor %edx, %edx # extend EAX to quadword div %ebx # perform division by 10 add $'0', %dl # remainder intto ascii mov %dl, buf(%ecx) # store numeral in buffer loop nxdiv # again for other digits # insert decimal-point movb $'.', buf # overwrite leading zero rolw $8, buf # swap character-positions # display message mov $4, %eax # 'write' system-call ID mov $1, %ebx # standard-output device lea msg, %ecx # address of message mov len, %edx # length of message int $0x80 # invoke kernel service # advance to the next x-value incl x # increment the x-value cmp $10, x # is x-value below ten? jb again # yes, show its sq-root # terminate program mov $1, %eax # 'exit' system-call ID mov $0, %ebx # use zero as exit-code int $0x80 # invoke kernel service .global _start .end