//----------------------------------------------------------------- // bigbreak.s // // This program employs several ideas, proposed by students, // for finding the largest memory-address that can be set as // the end of this program's data-section by using the Linux // 'brk' system-call and then reports the memory-size added. // // to assemble: $ as bigbreak.s -o bigbreak.o // and to link: $ ld bigbreak.o -o bigbreak // // programmer: ALLAN CRUSE // written on: 29 MAR 2007 //----------------------------------------------------------------- # manifest constants .equ sys_BRK, 45 .equ sys_WRITE, 4 .equ sys_EXIT, 1 .equ STDOUT, 1 .section .data brk0: .long 0 # for initial brk-value brk1: .long 0 # for current brk-value brk2: .long 0 # for the new brk-value incr: .long 0x80000000 # for current increment msg: .ascii "init-brk=" buf0: .ascii "xxxxxxxx curr-brk=" buf1: .ascii "xxxxxxxx incr=" buf2: .ascii "xxxxxxxx total-added=" buf3: .ascii "xxxxxxxx \n" len: .int . - msg .section .text _start: # get the original value for the 'brk' address mov $sys_BRK, %eax xor %ebx, %ebx int $0x80 mov %eax, brk0 # format the original brk-address for display mov brk0, %eax lea buf0, %edi call eax2hex # initialize the two other brk-address fields mov brk0, %eax mov %eax, brk1 mov %eax, brk2 # main loop for the 'divide-and-conquer' algorithm shift: shrl $1, incr # halve the increment again: # use the newest brk-address as the current brk-address mov brk2, %eax mov %eax, brk1 # then try to increase the current brk-address mov $sys_BRK, %eax mov brk1, %ebx add incr, %ebx int $0x80 mov %eax, brk2 # format the current brk-address for display mov brk1, %eax lea buf1, %edi call eax2hex # format the current increment for display mov incr, %eax lea buf2, %edi call eax2hex # format the amount of memory added for display mov brk2, %eax sub brk0, %eax lea buf3, %edi call eax2hex # display the current buffers' contents mov $sys_WRITE, %eax mov $STDOUT, %ebx lea msg, %ecx mov len, %edx int $0x80 # quit if the current increment has reach zero cmpl $0, incr je finis # otherwise try again (using a smaller increment if necessary) mov brk2, %eax # was the newest brk-value cmp %eax, brk1 # same as former brk-value? jne again # no, retain same increment jmp shift # else halve that increment finis: # terminate this program mov $sys_EXIT, %eax xor %ebx, %ebx int $0x80 hex: .ascii "0123456789ABCDEF" eax2hex: # convert the value in EAX to a hexadecimal string at EDI pushal mov $8, %ecx nxnyb: rol $4, %eax mov %al, %bl and $0xF, %ebx mov hex(%ebx), %dl mov %dl, (%edi) inc %edi loop nxnyb popal ret .global _start .end