Gondil
Gondil

Reputation: 807

Assembly x86 how to determine if current page is full

Have problem with determining if page is full during char-per-char output large string.

I print large buffer from file to output char-per-char but want also check if the page is not full. I mean, if it is full, then print some message (for ex.: Page is full, If you want to continue to next page press any key.). And after key press I print another "page" of chars from buffer.

It is something like when you print text from file in unix, it will ask you to continue or wait cause you maybe read text printed.

Upvotes: 0

Views: 351

Answers (1)

Gondil, I made next code for you in EMU8086 (assembler 8086 with Intel syntax). It reads a file char by char displaying them. It counts every line break, after reading 24 line breaks it displays the message (Page full, press any key), then continues reading file :

.stack 100h
.data
filename    db 'myfile.txt',0  ;NOTICE DE ZERO AT THE END.
filehandler dw ?        
msj         db 'Page full. Press any key to continue...',13,10,'$'
mychar      db ' $'  ;CHAR FROM FILE. DOLLAR SIGN TO DISPLAY IT.
lines      db 0  ;COUNTER FOR LINES DISPLAYED.
.code          
;INITILIZE DATA SEGMENT.
  mov  ax,@data
  mov  ds,ax

  call clear_screen

;OPEN FILE.
  mov  ah, 3dh  ;SERVICE TO OPEN FILE.
  mov  al, 0    ;MODE READ ONLY.
  mov  dx, offset filename
  int  21h
  mov  filehandler, ax

;DISPLAY FILE'S CHARACTERS STOPPING ON EVERY FULL PAGE.
repeat:
  ;READ ONE CHAR.
    mov  ah, 3fh  ;SERVICE TO READ FROM FILE.
    mov  bx, filehandler
    mov  cx, 1  ;HOW MANY BYTES TO READ.
    mov  dx, offset mychar ;VARIABLE TO STORE THE READ BYTE.
    int  21h  ;AX RETURNS WITH HOW MANY BYTES WHERE READ.
  ;CHECK EOF (END OF FILE).
    cmp  ax, 0
    je   eof
  ;DISPLAY CHAR.
    mov  ah, 9
    mov  dx, offset mychar
    int  21h
  ;FIND OUT IF CHAR IS LINE BREAK.    
    cmp  mychar, 13
    je   linebreak
    cmp  mychar, 10  ;IGNORE.
    je   notlinebreak
    jmp  notlinebreak
linebreak:            
    inc  lines
notlinebreak: 
  ;FIND OUT IF PAGE IS FULL.
    cmp  lines, 24
    je   pagefull
    jmp  notfull
pagefull:                            
    call page_is_full
    mov  lines, 0  ;COUNTER RESTARTS.
notfull:    
  jmp  repeat  ;REPEAT PROCESS FOR NEXT CHARACTER.
eof:
;CLOSE FILE.
  mov  ah, 3eh  ;SERVICE TO CLOSE FILE.
  mov  bx, filehandler
  int  21h

;FINISH PROGRAM PROPERLY.
  mov  ax,4c00h
  int  21h           
;==============================================================
;DISPLAY MESSAGE OF FULL PAGE AND WAIT FOR KEY.
proc page_is_full
;DISPLAY MESSAGE.
  mov  ah, 9
  mov  dx, offset msj
  int  21h
;STOP UNTIL USER PRESSES ANY KEY.      
  mov  ah, 7
  int  21h      
  ret
endp  
;--------------------------------------------------------------
proc clear_screen
  mov  ah,0  ;SERVICE TO SET SCREEN MODE.
  mov  al,3  ;MODES 0..7 ARE TEXT SCREEN.
  int  10H  ;BIOS SCREEN SERVICES.
  ret
endp

I am using Microsoft's line break (detecting character 13 and ignoring 10), you can change it for Unix style (detecting character 10 and ignoring 13).

If some text lines of the file were too long, just add another counter for the line length, when it reaches 79 characters, increase the "lines" counter.

Hope it helps you.

Upvotes: 1

Related Questions