Coding on the PC: lesson three - Lesson three: we already know how to load a file, now let’s try to _upload_ it.

Heresy #04


###################### ####################                      
######### ########## ######### ###                      
################### ########                                  
######### ######### ####### ###### ##### #######       
######### ########## ####### ###### ###### #       
###################### ######## # ####### #       
######### ##########                                  
######### ########## ###                      
######### a$h ####################                      

(c) Demiurge Ash                                                

    Lesson three: we already ymeem upload a file,                   
    Now let's try to _unload_ it.                           
|   Explanations are immediately given in the source text.                 
+----------------------- - -                                    

;---------------PROGRAM N1--------------------------------------------------
   .MODEL tiny                                                  
   .CODE                                                        
        ORG 100h                                                
start:                                                          
;--open file-----------                                   
       xor cx,cx ;setting attributesfile           
                           ; cx = 00h - regular file            
 ;      01h - read only       
                           ;      02h - hidden file            
                           ;      04h - system               
       mov dx,offset FILE ;pointer to an ASCII string,          
                           ;ending with zero, in            
                           ;which specifies the path to the file         
       mov ax,ЗCOOh ;preparing a function call           
                           ;DOS to create a file              
                           ;(if there is already a file with the same      
                           ;name and it has no attribute        
                           ;read-only, then it is destroyed)  
       int 21h ;function call                       
       jc no_file                                               
;--write file--------------                                   
       mov bx,ax ;pass the descriptor number          
       push bx ;remember the descriptor               
       mov cx,LEN ;number of bytes to write              
 mov dx,offset DATA ;buffer address                        
       mov ah,40h ;preparing a function call;DOS to write a file                
       int 21h ;function call                       
;--closing file-----------                                   
       pop bx ;remember the descriptor               
       mov ah,ЗEh ;norotobka function call            
                           ;DOS closing the file                  
       int 21h ;function call                       
;--exit---------------------                                   
exit:                                                           
       mov ah,Chch                                               
       int 21h ;exit to DOS                         

;--print message----------                                   
no_file:                                                        
       mov dx,offset MessЗ;first message address             
print:                                                          
       mov ah,9                                                 
       int 21h ;print messages                    
       jmp exit                                                 
;---data-------------------                                   

MessЗ db 'I cant save file -your.dat- !!!',13,10,'$'          
FILE db 'your.dat',0                                         
DATA db 'a should be herethere is your data, ',13,10      
        db 'which you want to write to a file...',13,10         
DATAEND db 0                                                    
LEN=DATAEND-DATA ;calculate the size of the data being written off    
                           ;buffers. The LEN variable is used 
                           ;in the file writing procedure.           
;----------------------------                                   
end start                                                       


+--------------------- --- -- - - -                            
|                                                               
    All this can be done in any editor (very convenient for this
built into Dos Navigator), then written to disk, to file c
extension, for example, saver.asm                               
    Now, if you have an assembler, you can compile it in
com file. Through the command line we set:                       
    tasmx /m2 saver.asm                                         
and get the object file saver.obj, link it to .com:        
    tlink /t /x saver.obj                                       
and see the new file saver.com - launch it!                  

 --------------------------------- -                           

     Now let's try to change the information in existing
files, for example, let's docamomodifying program,
which will change the ee launch counter in its file.       
     At the same time, we’ll make a procedure that prints out the number found
in hexadecimal form, as well as a procedure that displays text c
attributes.                                                     

;---------------PROGRAM N2--------------------------------------------------
   .MODEL tiny                                                  
   .CODE                                                        
        ORG 100h                                                
start:                                                          
;--open file-----------                                   
       mov ax,3d01h ;preparing a DOS function call       
                           ;to open a file                  
                           ;(AL=01h - for recording only)        
       mov dx,offset FILE ;path to 'our' file               
       int 21h ;function call                       
       jb no_file ;if there is no such file, then go 
 ;on file opening error            
;--move the pointer-----                                   
       mov bx,ax ;pass the descriptor number          
       push bx ;remember the descriptor               
       movax,4200h ;preparing a function call           
                           ;move the pointer in the file       
                           ;(AL=00h - relative to the beginning of the file)
       xor cx,cx ;pair CX:DX indicates the number of bytes per 
       mov dx,Count-Start ;which needs to move the pointer     
                           ;In our case, to the Count variable. 
       int 21h ;function call                       
;--change counter--------                                   
       inc [Count] ;increase the counter, the size of     
                           ;word                               
;--print counter----------                                   

       mov dx,offset MessS ;preface address                   
 mov bl,2                                                 
       call print ;preface print                  

       xor si,si ;reset index                   
       mov ax,[Count] ;take the counter                       
       mov bx,offset Buff ;denotes buffer                    
       mov cx,10 ;send the divisor                   
con1: mov dx,0 ;zero the register for the next    
                           ;sending the remainder into it              
       div cx ;AL=AL/СХ, a remainder in DX            
       add dl,'0';add the number code '0' to the remainder  
       mov [bx+si],dl ;and throw it into the buffer                
       inc si ;increase the index                  
       or ax,ax ;checking, maybe already completed    
                           ;to zero?                            
       jnz con1 ;if not, then divide further          
con2: dec si ;decrease index                    
 mov dl,[bx+si] ;get the number                       
       mov ah,2 ;preparing the symbol output function   
                           ;located in DL                   
       int 21h ;function call                       
       or si,si ;if the index is zero, then print more
                           ;nothing                              
       jnz con2 ; jump if there is something to print     

       mov dx,offset Messб;afterword address                   
       movbl,2                                                 
       call print                                               

;--write to file------------                                   
       pop bx ;remember the descriptor               
       mov ah,40h ;preparing a call to the recording function    
                           ;to file                              
       mov dx,offsetCount ;buffer address                        
 mov cx,2 ;number of bytes to write              
       int 21h ;function call                       
;--exit---------------------                                   
exit:                                                           
       mov ah,Chch                                               
       int 21h ;exit to DOS                         
;--print and exit-----------                                   
no_file:                                                        
       mov dx,offset MessР;address of the first message             
       mov bl,12                                                
       call print                                               
       jmp exit                                                 
;--print messages---------                                   
print: call print1 ;call the main procedure for        
                           ;printout message                
       mov dx,offset Standard ;print space co        
       mov bl,07h ;standard attributes for         
 ;recovery                      

print1:                                                         
       mov si,dx ;text address to index registercld ;indicate the direction of change     
                           ;index register: "increase"   
next_symbol:                                                    
       lodsb ;send byte from [SI] to AL          
       or al,al ;if zero, then                
       jz p_exit ;this is the end of the message                 
       mov ah,9 ;function for printing a symbol with an attribute  
       xor bh,bh ;video page number - 0             
       mov cx,1 ;number of characters - 1                 
       cmp al,1Fh ;check the symbol for the control code 
       jbe sys_symbol ;if yes, then skip printing       
                           ;attributes                           
       int 10h ;function call                       
sys_symbol:                                                     
       mov ah,OEh ;teletype function - execution      
                           ;control codes and movement    
                           ;cursor                             
       int 10h ;function call                       
       jmp next_symbol ;next symbol                    
p_exit:                                                         
       ret ;exit procedure;---data-------------------                                   

MessХ db 'I cant change file -our.com-',13,10,0               
MessS db 'Program was started',0                              
Messб db 'times',13,10,0                                     
Standard db 20h,0                                               
FILE db 'our.com',0                                          
Count dw 0000h                                                
Buff db 5 DUP (?)                                            
;----------------------------                                   
end start                                                       

+--------------------- --- -- - - -                            
| Through the command line we set:                               
    tasmx /m2 our.asm                                           
and get the object file our.obj, link it to .com:          
    tlink /t /x our.obj                                         
As a result, the file our.com should appear.                          
 --------------------------------- -                           

     Basically, if you know how to code on the Spectrum, it's not for you
It will be a lot of work to learn how to write decent programs in
PC.  The main thing here is to figure out how to work with the hardware
computer: with a screw, with a screen, with an audio card, etc., and allthe algorithms remain the same. Only the mnemonics of their recording change,
Well, some nuances in connection with the new teams. So for example 
the familiar “magic formula for information transfer” ;-)      

       LD HL,SOURCE                                             
       LD DE,DESTINATION                                        
       LD BC,SIZE                                               
       LDIR                                                     

     turns into a construction like:                           

      cld ;indicate the direction          
                                ;index register changes: 
                                ;"increase". It can be        
                                ;change to STD - "decrease" 
                                ;(i.e. LDDR direction)        
      mov si,offset Source ;remember that the data is taken from 
      mov di,offset Destination ;register pairs DS:SI and         
                                ;throw to the address ES:DI and     
 ;may be required           
                                ;adjustment of DS and ES registers
      mov cx,How ;number of bytes sent, a    
                                ;more precisely the number of command repetitions 
      rep movsb ;prefixrep is         
                                ;repetition prefix, she      
                                ;forces the next command   
                                ;execute CX times.            
                                ;And finally, the movsb command       
                                ;sends a byte. Instead    
                                ;can also be used       
                                ;command movsw - forwarding      
                                ;words                          
     In one of Harm's rooms Dr.Shizer offers to replace
team                                                         
     ...                                                        
     rep movsb                                                  

     to a design like                                        

     ...                                                        
     shr cx,1                                                   
     rep movsw                                                  
     adc cx,cx                                                  
     rep movsb                                                  

     i.e.  We throw everything we can in words, and the rest in bytes.
Loss in size, gain in time!                          

Share your thoughts about the article