Search This Blog

Thursday, January 24, 2013

Assignment on 80386 from prevoius year papers with answers


Assignment No 1 [with minimum answer]

[Should be submitted in separate homework notebook of 100 pages by 31st January, 2013]

1.      What is Machine Status Word (MSW) of 80386? Draw its format.
Ans:
  Figure : Control Register 0 ( Machine Status Word ) of 80386

PG (Paging Enable, bit 31)
The PG bit is set to enable the on-chip paging unit. It is reset to disable the on-chip paging unit.
R (reserved, bit 4)
This bit is reserved by Intel. When loading CR0 care should be taken to not alter the value of this bit.
TS (Task Switched, bit 3)
TS is automatically set whenever a task switch operation is performed. If TS is set, a coprocessor ESCape opcode will cause a Coprocessor Not Available trap (exception 7). The trap handler typically saves the Intel387 DX coprocessor context belonging to a previous task, loads the Intel387 DX coprocessor state belonging to the current task, and clears the TS bit before returning to the faulting coprocessor opcode.
EM (Emulate Coprocessor, bit 2)
The EMulate coprocessor bit is set to cause all coprocessor opcodes to generate a Coprocessor Not Available fault (exception 7). It is reset to allow coprocessor opcodes to be executed on an actual Intel387 DX coprocessor. Note that theWAIT opcode is not affected by the EM bit setting.
MP (Monitor Coprocessor, bit 1)
The MP bit is used in conjunction with the TS bit to determine if the WAIT opcode will generate a Coprocessor Not Available fault (exception 7) when TS e 1. When both MP e 1 and TS e 1, the WAIT opcode generates a trap. Otherwise, the WAIT opcode does not generate a trap. Note that TS is automatically set whenever a task switch operation is performed.
PE (Protection Enable, bit 0)
The PE bit is set to enable the Protected Mode. If PE is reset, the processor operates again in Real Mode. PE may be set by loading MSW or CR0. PE can be reset only by a load into CR0. Resetting the PE bit is typically part of a longer instruction sequence needed for proper transition from Protected Mode to Real Mode.
2.   With the help of block diagram explain the architecture of 80386.
Ans:

Figure : 80386 Architecture block Diagram
 
 [Explanation should include around one sentence about each block in above figure.]


3.      What is the function of BE0 to BE3 signal in 80386?
                        Ans:
The 386 address bus consists of the A2-A31 address lines and the byte enable lines BE0# - BE3#.  The BE0#-BE3# lines are decoded from internal address signals A0 and A1 and function very similarly to the way A0 and BHE function in an 8086 or 80286 system.  The 80386 has a 32-bit data bus, so memory can be set up as four byte-wide banks.  The BE0# - BE3# signals function as enables for the four banks.  These individual enables allow the 386 to transfer bytes, words, or double words to and from memory.  Incidentally, the # symbol after the BE signal names indicates that these signals are active low.
[Draw table of mapping A0, A1 with this lines]
4.            Explain BIU of 80386.
Ans:
[Draw part of architecture corresponding to Bus Interface.]
[Explain all pins connected to BIU.]
5.            Explain interrupt pins of 80386.
Ans:
 1. INTR:  INTERRUPT REQUEST is a maskable input that signals the 80386 to suspend execution of the current program and execute an interrupt acknowledge function.
2. NMI: NON-MASKABLE INTERRUPT REQUEST is a non-maskable input that signals the 80386 to suspend execution of the current program and execute an interrupt acknowledge function.
3. RESET:  RESET suspends any operation in progress and places the 80386 in a known reset state. See Interrupt Signals for additional information.

6.      Explain debug registers in 80386.
Ans:
The six programmer accessible debug registers provide on-chip support for debugging.  Debug Registers DR0- specify the four linear breakpoints. The Debug Control Register DR7 is used to set the breakpoints and the Debug Status Register DR6, displays the current state of the breakpoints.

7.      Explain Difference between 8086 and 80386.
Ans:
[Find out at least 8 points]

Tuesday, January 22, 2013

Flag Register of 80386 micro processor



Flag Register of 80386

  [ source: Intel386 DX Manual]


Note in these descriptions, ``set'' means ``set to 1,'' and ``reset'' means ``reset to 0.''


 VM (Virtual 8086 Mode, bit 17)

The VM bit provides Virtual 8086 Mode within Protected Mode. If set while the 80386 is in Protected Mode, the 80386 will switch to Virtual 8086 operation, handling segment loads as the 8086 does, but generating exception 13 faults on privileged opcodes.

RF (Resume Flag, bit 16)

The RF flag is used in conjunction with the debug register breakpoints. It is checked at instruction boundaries before breakpoint processing. When RF is set, it causes any debug fault to be ignored on the next instruction. RF is then automatically reset at the successful completion of every instruction.

NT (Nested Task, bit 14)

This flag applies to Protected Mode. NT is set to indicate that the execution of this task is nested within another task. If set, it indicates that the current nested task's Task State Segment (TSS) has a valid back link to the previous task's TSS. This bit is set or reset by control transfers to other tasks.

IOPL (Input/Output Privilege Level, bits 12-13)

This two-bit field applies to Protected Mode. IOPL indicates the numerically maximum CPL (current privilege level) value permitted to execute I/O instructions without generating an exception 13 fault or consulting the I/O Permission Bitmap. It also indicates the maximum CPL value allowing alteration of the IF (INTR Enable Flag) bit when new values are popped into the EFLAG register.

OF (Overflow Flag, bit 11)

OF is set if the operation resulted in a signed overflow. Signed overflow occurs when the operation resulted in carry/borrow into the sign bit (high-order bit) of the result but did not result in a carry/borrow out of the high order bit, or vice-versa. For 8/16/32 bit operations, OF is set according to overflow at bit 7/15/31, respectively.

DF (Direction Flag, bit 10)

DF defines whether ESI and/or EDI registers post decrement or post increment during the string instructions. Post increment occurs if DF is reset. Post decrement occurs if DF is set.

IF (INTR Enable Flag, bit 9)

The IF flag, when set, allows recognition of external interrupts signaled on the INTR pin.
When IF is reset, external interrupts signaled on the INTR are not recognized. IOPL indicates the maximum CPL value allowing alteration of the IF bit when new values are popped into EFLAGS or FLAGS.

TF (Trap Enable Flag, bit 8)

TF controls the generation of exception 1trap when single-stepping through code. When TF is set, the 80386 generates an exception 1 trap after the next instruction is executed. When TF is reset, exception 1 traps occur only as a function of the breakpoint addresses loaded into debug registers DR0±DR3.

SF (Sign Flag, bit 7)

SF is set if the high-order bit of the result is set, it is reset otherwise. For 8-, 16-, 32-bit operations, SF reflects the state of bit 7, 15, 31 respectively.

ZF (Zero Flag, bit 6)

ZF is set if all bits of the result are 0. Otherwise it is reset.

AF (Auxiliary Carry Flag, bit 4)

The Auxiliary Flag is used to simplify the addition and subtraction of packed BCD quantities. AF is set if the operation resulted in a carry out of bit 3 (addition) or a borrow into bit 3 (subtraction). Otherwise AF is reset. AF is affected by carry out of, or borrow into bit 3 only, regardless of overall operand length: 8, 16 or 32 bits.

PF (Parity Flags, bit 2)

PF is set if the low-order eight bits of the operation contains an even number of ``1's'' (even parity). PF is reset if the low-order eight bits have odd parity. PF is a function of only the low-order eight bits, regardless of operand size.

CF (Carry Flag, bit 0)

CF is set if the operation resulted in a carry out of (addition), or a borrow into (subtraction) the high-order bit. Otherwise CF is reset. For 8-, 16- or 32-bit operations, CF is set according to carry/borrow at bit 7, 15 or 31, respectively.


Thursday, January 10, 2013

Read number from user in assembly language of 8086


;Read number from user in assembly language of 8086

DATA SEGMENT
          FIRST DB 00H
DATA ENDS
CODE SEGMENT
    ASSUME CS:CODE, DS:DATA
 START:
      MOV AX, DATA
      MOV DS, AX
READMORE:
      MOV AH,01H
      INT 21H
      CMP AL,'0'
      JB NOMOREREAD
      CMP AL, '9'
      JA NOMOREREAD
      MOV BL,AL
      MOV AH,10
      MUL FIRST
      SUB BL,30H
      ADD AH, BL
      MOV FIRST, AH
      JMP READMORE
 NOMOREREAD:
      ; THIS IS TO TERMINATE AND GIVE CONTROL TO DOS
      MOV AH, 4CH
      INT 21H

CODE ENDS
 END START

Monday, January 7, 2013

Program to find out factorial of number in 8086 assembly language

;Program to find out factorial of number in 8086 assembly language

data segment
     number dw 04h
ends

stack segment
    dw   128  dup(0)
ends

code segment
    assume cs:code, ds:data
start:
    ; set segment registers:
    mov ax, data
    mov ds, ax
 
    mov cx,number
    call fact         
    mov ax, 4c00h ; exit to operating system.
    int 21h 
   
    ;procedure for factorial program
    ;assume cx contains input number
    ;assume Dx contains result 
    fact proc near
           cmp cx, 01h
           jne cont
           mov dx,01h
           ret
  cont:    push cx ; for backup
           dec cx  
           call fact 
           pop ax    ; backup of cx ie n
           mul dx    ; n*(n-1)!
           mov dx, ax ; result into dx
           ret                                          
    fact endp
ends

end start ; set entry point and stop the assembler.




Sunday, January 6, 2013

Interrupts list for 80386 microprocessor


Source : www.emu8086.com
Recommended for 8086 programming Practice.
_______________________________________
INT 10h / AH = 0 - set video mode.
input:
AL = desired video mode.

these video modes are supported:

00h - text mode. 40x25. 16 colors. 8 pages.

03h - text mode. 80x25. 16 colors. 8 pages.

13h - graphical mode. 40x25. 256 colors. 320x200 pixels. 1 page.
example:

 mov al, 13h
 mov ah, 0
 int 10h


INT 10h / AH = 01h - set text-mode cursor shape.
input:
CH = cursor start line (bits 0-4) and options (bits 5-7).
CL = bottom cursor line (bits 0-4).

when bit 5 of CH is set to 0, the cursor is visible. when bit 5 is 1, the cursor is not visible.

; hide blinking text cursor:
      mov ch, 32
      mov ah, 1
      int 10h

; show standard blinking text cursor:
      mov ch, 6
      mov cl, 7
      mov ah, 1
      int 10h

; show box-shaped blinking text cursor:
      mov ch, 0
      mov cl, 7
      mov ah, 1
      int 10h

;      note: some bioses required CL to be >=7,
;      otherwise wrong cursor shapes are displayed.
 
________________________________________
INT 10h / AH = 2 - set cursor position.
input:
DH = row.
DL = column.
BH = page number (0..7).
example:

 mov dh, 10
 mov dl, 20
 mov bh, 0
 mov ah, 2
 int 10h
________________________________________
INT 10h / AH = 03h - get cursor position and size.
input:
BH = page number.
return:
DH = row.
DL = column.
CH = cursor start line.
CL = cursor bottom line.
________________________________________
INT 10h / AH = 05h - select active video page.
input:
AL = new page number (0..7).
the activated page is displayed.

________________________________________
INT 10h / AH = 06h - scroll up window.
INT 10h / AH = 07h - scroll down window.
input:
AL = number of lines by which to scroll (00h = clear entire window).
BH = attribute used to write blank lines at bottom of window.
CH, CL = row, column of window's upper left corner.
DH, DL = row, column of window's lower right corner.
________________________________________
INT 10h / AH = 08h - read character and attribute at cursor position.
input:
BH = page number.
return:
AH = attribute.
AL = character.
________________________________________
INT 10h / AH = 09h - write character and attribute at cursor position.
input:
AL = character to display.
BH = page number.
BL = attribute.
CX = number of times to write character.
________________________________________
INT 10h / AH = 0Ah - write character only at cursor position.
input:
AL = character to display.
BH = page number.
CX = number of times to write character.
________________________________________
INT 10h / AH = 0Ch - change color for a single pixel.
input:
AL = pixel color
CX = column.
DX = row.
example:

 mov al, 13h
 mov ah, 0
 int 10h     ; set graphics video mode.
 mov al, 1100b
 mov cx, 10
 mov dx, 20
 mov ah, 0ch
 int 10h     ; set pixel.
________________________________________
INT 10h / AH = 0Dh - get color of a single pixel.
input:
CX = column.
DX = row.
output:
AL = pixel color
________________________________________
INT 10h / AH = 0Eh - teletype output.
input:
AL = character to write.
this functions displays a character on the screen, advancing the cursor and scrolling the screen as necessary. the printing is always done to current active page.

example:

 mov al, 'a'
 mov ah, 0eh
 int 10h

 ; note: on specific systems this
 ; function may not be supported in graphics mode.
________________________________________
INT 10h / AH = 13h - write string.
input:
AL = write mode:
    bit 0: update cursor after writing;
    bit 1: string contains attributes.
BH = page number.
BL = attribute if string contains only characters (bit 1 of AL is zero).
CX = number of characters in string (attributes are not counted).
DL,DH = column, row at which to start writing.
ES:BP points to string to be printed.
example:

 mov al, 1
 mov bh, 0
 mov bl, 0011_1011b
 mov cx, msg1end - offset msg1 ; calculate message size.
 mov dl, 10
 mov dh, 7
 push cs
 pop es
 mov bp, offset msg1
 mov ah, 13h
 int 10h
 jmp msg1end
 msg1 db " hello, world! "
 msg1end:
________________________________________
INT 10h / AX = 1003h - toggle intensity/blinking.
input:
BL = write mode:
    0: enable intensive colors.
    1: enable blinking (not supported by the emulator and windows command prompt).
BH = 0 (to avoid problems on some adapters).
example:

mov ax, 1003h
mov bx, 0
int 10h
________________________________________

bit color table:
character attribute is 8 bit value, low 4 bits set fore color, high 4 bits set background color.
note: the emulator and windows command line prompt do not support background blinking, however to make colors look the same in dos and in full screen mode it is required to turn off the background blinking.

HEX    BIN        COLOR

0      0000      black
1      0001      blue
2      0010      green
3      0011      cyan
4      0100      red
5      0101      magenta
6      0110      brown
7      0111      light gray
8      1000      dark gray
9      1001      light blue
A      1010      light green
B      1011      light cyan
C      1100      light red
D      1101      light magenta
E      1110      yellow
F      1111      white
note:

; use this code for compatibility with dos/cmd prompt full screen mode:
mov     ax, 1003h
mov     bx, 0   ; disable blinking.
int     10h

________________________________________

INT 11h - get BIOS equipment list.
return:
AX = BIOS equipment list word, actually this call returns the contents of the word at 0040h:0010h.

Currently this function can be used to determine the number of installed number of floppy disk drives.

Bit fields for BIOS-detected installed hardware:
bit(s) Description
 15-14  Number of parallel devices.
 13     Reserved.
 12     Game port installed.
 11-9   Number of serial devices.
 8      Reserved.
 7-6    Number of floppy disk drives (minus 1):
          00 single floppy disk;
          01 two floppy disks;
          10 three floppy disks;
          11 four floppy disks.
 5-4    Initial video mode:
          00 EGA,VGA,PGA, or other with on-board video BIOS;
          01 40x25 CGA color.
          10 80x25 CGA color (emulator default).
          11 80x25 mono text.
 3      Reserved.
 2      PS/2 mouse is installed.
 1      Math coprocessor installed.
 0      Set when booted from floppy.
________________________________________

INT 12h - get memory size.
return:
AX = kilobytes of contiguous memory starting at absolute address 00000h, this call returns the contents of the word at 0040h:0013h.
________________________________________

Floppy drives are emulated using FLOPPY_0(..3) files.





  ________________________________________

INT 13h / AH = 00h - reset disk system.
________________________________________

INT 13h / AH = 02h - read disk sectors into memory.
INT 13h / AH = 03h - write disk sectors.
input:
AL = number of sectors to read/write (must be nonzero)
CH = cylinder number (0..79).
CL = sector number (1..18).
DH = head number (0..1).
DL = drive number (0..3 , for the emulator it depends on quantity of FLOPPY_ files).
ES:BX points to data buffer.
return:
CF set on error.
CF clear if successful.
AH = status (0 - if successful).
AL = number of sectors transferred.
Note: each sector has 512 bytes.
________________________________________

INT 15h / AH = 86h - BIOS wait function.
input:
CX:DX = interval in microseconds
return:
CF clear if successful (wait interval elapsed),
CF set on error or when wait function is already in progress.

Note:
the resolution of the wait period is 977 microseconds on many systems (1 million microseconds - 1 second).
Windows XP does not support this interrupt (always sets CF=1).

________________________________________

INT 16h / AH = 00h - get keystroke from keyboard (no echo).
return:
AH = BIOS scan code.
AL = ASCII character.
(if a keystroke is present, it is removed from the keyboard buffer).
________________________________________

INT 16h / AH = 01h - check for keystroke in the keyboard buffer.
return:
ZF = 1 if keystroke is not available.
ZF = 0 if keystroke available.
AH = BIOS scan code.
AL = ASCII character.
(if a keystroke is present, it is not removed from the keyboard buffer).
________________________________________

INT 19h - system reboot.
Usually, the BIOS will try to read sector 1, head 0, track 0 from drive A: to 0000h:7C00h. The emulator just stops the execution, to boot from floppy drive select from the menu: 'virtual drive' -> 'boot from floppy'
________________________________________

INT 1Ah / AH = 00h - get system time.
return:
CX:DX = number of clock ticks since midnight.
AL = midnight counter, advanced each time midnight passes.
notes:
there are approximately 18.20648 clock ticks per second,
and 1800B0h per 24 hours.
AL is not set by the emulator. 
________________________________________


INT 20h - exit to operating system.

________________________________________


The short list of emulated MS-DOS interrupts -- INT 21h
________________________________________
DOS file system is emulated in C:\emu8086\vdrive\x (x is a drive letter)

If no drive letter is specified and current directory is not set, then C:\emu8086\MyBuild\ path is used by default. FLOPPY_0,1,2,3 files are emulated independently from DOS file system.

For the emulator physical drive A: is this file c:\emu8086\FLOPPY_0 (for BIOS interrupts: INT 13h and boot).

For DOS interrupts (INT 21h) drive A: is emulated in this subdirectory: C:\emu8086\vdrive\a\

Note: DOS file system limits the file and directory names to 8 characters, extension is limited to 3 characters;
example of a valid file name: myfile.txt (file name = 6 chars, extension - 3 chars). extension is written after the dot, no other dots are allowed.  
________________________________________

INT 21h / AH=1 - read character from standard input, with echo, result is stored in AL.
if there is no character in the keyboard buffer, the function waits until any key is pressed.

example:

 mov ah, 1
 int 21h

________________________________________


INT 21h / AH=2 - write character to standard output.
entry: DL = character to write, after execution AL = DL.

example:

 mov ah, 2
 mov dl, 'a'
 int 21h
________________________________________


INT 21h / AH=5 - output character to printer.
entry: DL = character to print, after execution AL = DL.

example:

 mov ah, 5
 mov dl, 'a'
 int 21h
________________________________________
INT 21h / AH=6 - direct console input or output.

parameters for output: DL = 0..254 (ascii code)
parameters for input: DL = 255

for output returns: AL = DL
for input returns: ZF set if no character available and AL = 00h, ZF clear if character available.
AL = character read; buffer is cleared.

example:

 mov ah, 6
 mov dl, 'a'
 int 21h       ; output character.

 mov ah, 6
 mov dl, 255
 int 21h       ; get character from keyboard buffer (if any) or set ZF=1.

________________________________________

INT 21h / AH=7 - character input without echo to AL.
if there is no character in the keyboard buffer, the function waits until any key is pressed.

example:

 mov ah, 7
 int 21h
________________________________________
INT 21h / AH=9 - output of a string at DS:DX. String must be terminated by '$'.

example:

  org 100h
  mov dx, offset msg
  mov ah, 9
  int 21h
  ret
  msg db "hello world $"

________________________________________


INT 21h / AH=0Ah - input of a string to DS:DX, fist byte is buffer size, second byte is number of chars actually read. this function does not add '$' in the end of string. to print using INT 21h / AH=9 you must set dollar character at the end of it and start printing from address DS:DX + 2.

example:

  org 100h
  mov dx, offset buffer
  mov ah, 0ah
  int 21h
  jmp print
  buffer db 10,?, 10 dup(' ')
  print:
  xor bx, bx
  mov bl, buffer[1]
  mov buffer[bx+2], '$'
  mov dx, offset buffer + 2
  mov ah, 9
  int 21h
  ret
the function does not allow to enter more characters than the specified buffer size.
see also int21.asm in c:\emu8086\examples

________________________________________


INT 21h / AH=0Bh - get input status;
returns: AL = 00h if no character available, AL = 0FFh if character is available.
________________________________________

INT 21h / AH=0Ch - flush keyboard buffer and read standard input.
entry: AL = number of input function to execute after flushing buffer (can be 01h,06h,07h,08h, or 0Ah - for other values the buffer is flushed but no input is attempted); other registers as appropriate for the selected input function.
________________________________________

INT 21h / AH= 0Eh - select default drive.

Entry: DL = new default drive (0=A:, 1=B:, etc)

Return: AL = number of potentially valid drive letters

Notes: the return value is the highest drive present.
________________________________________


INT 21h / AH= 19h - get current default drive.

Return: AL = drive (0=A:, 1=B:, etc)

________________________________________


INT 21h / AH=25h - set interrupt vector;
input: AL = interrupt number. DS:DX -> new interrupt handler.
  ________________________________________

INT 21h / AH=2Ah - get system date;
return: CX = year (1980-2099). DH = month. DL = day. AL = day of week (00h=Sunday)
________________________________________
INT 21h / AH=2Ch - get system time;
return: CH = hour. CL = minute. DH = second. DL = 1/100 seconds.
________________________________________
INT 21h / AH=35h - get interrupt vector;
entry: AL = interrupt number;
return: ES:BX -> current interrupt handler.
________________________________________
INT 21h / AH= 39h - make directory.
entry: DS:DX -> ASCIZ pathname; zero terminated string, for example:

org 100h
mov dx, offset filepath
mov ah, 39h
int 21h

ret

filepath DB "C:\mydir", 0  ; path to be created.
end
the above code creates c:\emu8086\vdrive\C\mydir directory if run by the emulator.

Return: CF clear if successful AX destroyed. CF set on error AX = error code.
Note: all directories in the given path must exist except the last one.
________________________________________
INT 21h / AH= 3Ah - remove directory.

Entry: DS:DX -> ASCIZ pathname of directory to be removed.

Return:

CF is clear if successful, AX destroyed CF is set on error AX = error code.

Notes: directory must be empty (there should be no files inside of it).
________________________________________
INT 21h / AH= 3Bh - set current directory.

Entry: DS:DX -> ASCIZ pathname to become current directory (max 64 bytes).

Return:

Carry Flag is clear if successful, AX destroyed.
Carry Flag is set on error AX = error code.
Notes: even if new directory name includes a drive letter, the default drive is not changed,
only the current directory on that drive.
________________________________________
INT 21h / AH= 3Ch - create or truncate file.

entry:

CX = file attributes:

 mov cx, 0       ;  normal - no attributes.
 mov cx, 1       ;  read-only.
 mov cx, 2       ;  hidden.
 mov cx, 4       ;  system
 mov cx, 7       ;  hidden, system and read-only!
 mov cx, 16      ;  archive
DS:DX -> ASCIZ filename.

returns:

CF clear if successful, AX = file handle.
CF set on error AX = error code.

note: if specified file exists it is deleted without a warning.

example:

 org 100h
 mov ah, 3ch
 mov cx, 0
 mov dx, offset filename
 mov ah, 3ch
 int 21h
 jc err
 mov handle, ax
 jmp k
 filename db "myfile.txt", 0
 handle dw ?
 err:
 ; ....
 k:
 ret
________________________________________
INT 21h / AH= 3Dh - open existing file.

Entry:

AL = access and sharing modes:


mov al, 0   ; read
mov al, 1   ; write
mov al, 2   ; read/write


DS:DX -> ASCIZ filename.

Return:

CF clear if successful, AX = file handle.
CF set on error AX = error code.

note 1: file pointer is set to start of file.
note 2: file must exist.

example:

 org 100h
 mov al, 2
 mov dx, offset filename
 mov ah, 3dh
 int 21h
 jc err
 mov handle, ax
 jmp k
 filename db "myfile.txt", 0
 handle dw ?
 err:
 ; ....
 k:
 ret
________________________________________
INT 21h / AH= 3Eh - close file.

Entry: BX = file handle

Return:

CF clear if successful, AX destroyed.
CF set on error, AX = error code (06h).
________________________________________

INT 21h / AH= 3Fh - read from file.

Entry:

BX = file handle.
CX = number of bytes to read.
DS:DX -> buffer for data.

Return:

CF is clear if successful - AX = number of bytes actually read; 0 if at EOF (end of file) before call.
CF is set on error AX = error code.

Note: data is read beginning at current file position, and the file position is updated after a successful read the returned AX may be smaller than the request in CX if a partial read occurred.
________________________________________
INT 21h / AH= 40h - write to file.

entry:

BX = file handle.
CX = number of bytes to write.
DS:DX -> data to write.

return:

CF clear if successful; AX = number of bytes actually written.
CF set on error; AX = error code.

note: if CX is zero, no data is written, and the file is truncated or extended to the current position data is written beginning at the current file position, and the file position is updated after a successful write the usual cause for AX < CX on return is a full disk.
________________________________________
INT 21h / AH= 41h - delete file (unlink).

Entry:

DS:DX -> ASCIZ filename (no wildcards, but see notes).

return:

CF clear if successful, AX destroyed. AL is the drive of deleted file (undocumented).
CF set on error AX = error code.

Note: DOS does not erase the file's data; it merely becomes inaccessible because the FAT chain for the file is cleared deleting a file which is currently open may lead to filesystem corruption.
________________________________________
INT 21h / AH= 42h - SEEK - set current file position.

Entry:

AL = origin of move: 0 - start of file. 1 - current file position. 2 - end of file.
BX = file handle.
CX:DX = offset from origin of new file position.

Return:

CF clear if successful, DX:AX = new file position in bytes from start of file.
CF set on error, AX = error code.

Notes:

for origins 1 and 2, the pointer may be positioned before the start of the file; no error is returned in that case, but subsequent attempts to read or write the file will produce errors. If the new position is beyond the current end of file, the file will be extended by the next write (see AH=40h).

example:

 org 100h
 mov ah, 3ch
 mov cx, 0
 mov dx, offset filename
 mov ah, 3ch
 int 21h  ; create file...
 mov handle, ax

 mov bx, handle
 mov dx, offset data
 mov cx, data_size
 mov ah, 40h
 int 21h ; write to file...

 mov al, 0
 mov bx, handle
 mov cx, 0
 mov dx, 7
 mov ah, 42h
 int 21h ; seek...

 mov bx, handle
 mov dx, offset buffer
 mov cx, 4
 mov ah, 3fh
 int 21h ; read from file...

 mov bx, handle
 mov ah, 3eh
 int 21h ; close file...
 ret

 filename db "myfile.txt", 0
 handle dw ?
 data db " hello files! "
 data_size=$-offset data
 buffer db 4 dup(' ')
________________________________________
INT 21h / AH= 47h - get current directory.

Entry:

DL = drive number (00h = default, 01h = A:, etc)
DS:SI -> 64-byte buffer for ASCIZ pathname.

Return:

Carry is clear if successful
Carry is set on error, AX = error code (0Fh)

Notes:

the returned path does not include a drive and the initial backslash.
________________________________________
INT 21h / AH=4Ch - return control to the operating system (stop program).
________________________________________
INT 21h / AH= 56h - rename file / move file.

Entry:

DS:DX -> ASCIZ filename of existing file.
ES:DI -> ASCIZ new filename.

Return:

CF clear if successful.
CF set on error, AX = error code.

Note: allows move between directories on same logical drive only; open files should not be renamed!
________________________________________
mouse driver interrupts -- INT 33h
________________________________________
INT 33h / AX=0000 - mouse ininialization. any previous mouse pointer is hidden.

returns:
if successful: AX=0FFFFh and BX=number of mouse buttons.
if failed: AX=0
example:

mov ax, 0
int 33h
see also: mouse.asm in examples.
________________________________________
INT 33h / AX=0001 - show mouse pointer.

example:

mov ax, 1
int 33h
________________________________________
INT 33h / AX=0002 - hide visible mouse pointer.

example:

mov ax, 2
int 33h
________________________________________
INT 33h / AX=0003 - get mouse position and status of its buttons.

returns:
if left button is down: BX=1
if right button is down: BX=2
if both buttons are down: BX=3
CX = x
DX = y
example:

mov ax, 3
int 33h

; note: in graphical 320x200 mode the value of CX is doubled.
; see mouse2.asm in examples.




Structure of .COM file



A .COM file consists entirely of executable code and data. When the file Hello.COM is executed, for example (by typing either Hello or Hello.COM at the DOS prompt), the contents of the file are simply loaded into memory. When the file has been loaded, execution starts with the first byte. All of the segment registers are set to point to a single 64K segment starting 256 bytes before the address where the program was loaded, so in fact execution starts at CS:0100. The first 256 bytes of the segment comprise the Program Segment Prefix (PSP), which contains a variety of pieces of information about the executing program.
The most useful field in the PSP is the tail of the command line; for example, if Hello.COM had been executed by typing Hello Ram, then the string Ram would be stored in the PSP. The program can access this argument string starting at offset 80h; the first byte gives the length of the tail (3 in the example), and that many bytes starting at 81h contain the string itself. The string is terminated with a carriage return character (ASCII code 0Dh), which is not included in the count.
Since the entire segment registers point to the same segment, the structure of a typical .COM program in memory is as follows: 



The program text and initialized data are the bytes that are read in from the .COM file, corresponding to the code and data sections of the .asm source. The PSP is generated by the operating system, and the stack is automatically arranged to grow down from the top of the segment. The uninitialized data, corresponding to the bytes reserved are carved out of the free space between the loaded bytes and the growing stack; since they were not explicitly initialized before execution, they will start out containing whatever garbage was left in those locations of physical memory by the previous programs.