//------------------------------------------------------------------- // backward.s // // This text-mode demo relies on our 'dosio.ko' kernel module // to enable privileged access to i/o ports and video memory. // It illustrates use of an x86 i/o instruction, of the .rept // assembly language macro, and the Linux 'mmap' system-call. // // assemble using: $ as backward.s -o backward.o // and link using: $ ld backward.o -o backward // // programmer: ALLAN CRUSE // written on: 26 APR 2006 //------------------------------------------------------------------- # manifest constants .equ sys_exit, 1 # service-ID for 'exit' .equ sys_write, 4 # service-ID for 'write' .equ sys_open, 5 # service-ID for 'open' .equ sys_mmap, 90 # service-ID for 'mmap' .equ O_RDWR, 2 # read/write access .equ STDERR, 2 # device-ID for errors .equ PROT, 0x03 # readable, writable .equ FLAG, 0x11 # shared, fixed .section .data device: .asciz "/dev/dos" # null-terminated name packet: .int 0xA0000, 0x020000, PROT, FLAG # 'mmap' parameters handle: .int -1 # for device identifier offset: .int 0xA0000 # final 'mmap' parameter msg1: .ascii "Could not open \'/dev/dos\' \n" len1: .int . - msg1 # length of error-message msg2: .ascii "Could not map display-memory\n" len2: .int . - msg2 # length of error-message .section .text _start: # open the device-file mov $sys_open, %eax lea device, %ebx mov $2, %ecx int $0x80 mov %eax, handle # check that 'open' succeeded lea msg1, %ecx mov len1, %edx cmpl $-1, handle je fail # memory-map the device's video-memory mov $sys_mmap, %eax lea packet, %ebx int $0x80 # check that 'mmap' succeeded lea msg2, %ecx mov len2, %edx cmp $-1, %eax je exit #-------------------------------------------------------- # prolog: make the character-generater memory accessible #-------------------------------------------------------- mov $0x03C4, %dx # Timer-Sequencer mov $0x0100, %ax # enter synchronous reset out %ax, %dx mov $0x0402, %ax # write only to map 2 out %ax, %dx mov $0x0704, %ax # use sequential addressing out %ax, %dx mov $0x0300, %ax # leave synchronous reset out %ax, %dx mov $0x03CE, %dx # Graphics Controller mov $0x0204, %ax # select map 2 for reads out %ax, %dx mov $0x0005, %ax # disable odd-even addressing out %ax, %dx mov $0x0006, %ax # map starts at 0xA000:0000 out %ax, %dx #------------------------------------------------ # modify the contents of character-generator ram #------------------------------------------------ mov $0xA0000, %ebx # point to display memory mov $0x08000, %ecx # count is 32*256 xor %esi, %esi # initialize array-index nxflip: mov (%ebx, %esi, 1), %al # fetch glyph-row .rept 8 rol $1, %al # hi-bit to carry-flag rcr $1, %dl # carry-flag to lo-bit .endr mov %dl, (%ebx, %esi, 1) # store glyph-row inc %esi # advance array-index loop nxflip #---------------------------------------------------------- # epilog: make the character-generater memory inaccessible #---------------------------------------------------------- mov $0x03C4, %dx # Timer-Sequencer mov $0x0100, %ax # enter synchronous reset out %ax, %dx mov $0x0302, %ax # write to maps 0 and 1 out %ax, %dx mov $0x0304, %ax # use odd-even addressing out %ax, %dx mov $0x0300, %ax # leave synchronous reset out %ax, %dx mov $0x03CE, %dx # Graphics Controller mov $0x0004, %ax # select map 0 for reads out %ax, %dx mov $0x1005, %ax # enable odd-even addressing out %ax, %dx mov $0x0E06, %ax # map starts at 0xB800:0000 out %ax, %dx jmp exit # terminate this program fail: # show a specified failure-message mov $sys_write, %eax mov $STDERR, %ebx int $0x80 exit: # terminate this application mov $sys_exit, %eax mov $0, %ebx int $0x80 .global _start .end PROGRAMMING NOTES The following two references were extremely helpful in writing this demo-program in assembly language: ------- How to pass the six parameters in the 'mmap' system-call: M. Beck et al, "Linux Kernel Programming (Third Edition)," Addison-Wesley (2002), pp. 366-368. ------- How to reprogram the video hardware to change the glyphs stored in character-generator ram: Richard Wilton, "Programmer's Guide to PC Video Systems (Second Edition)," Microsoft Press (1994), pp. 294-299.