Einzelnen Beitrag anzeigen

tommie-lie
(Gast)

n/a Beiträge
 
#11

Re: Maschinensprache und Assembler - ein kleines Beispiel

  Alt 19. Okt 2005, 21:01
Wenn die Fragen nicht erwünscht sein sollten, haben die Moderatoren eine Funktion, um Beiträge abzusplitten.

Zitat von Dussel:
Dann eine weitere Frage: Gibt es das Programm auch als komplett eigenständiges Programm, so dass ich es z.B. auf einer leeren Festplatte oder auf nicht DOS basierenden Betriebssystemen ausführen könnte?
Ja, gibt es theoretisch:
Code:
/*
  Copyright (C) 2004  Thomas Liebetraut

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/


/*
  __History__:
  0.01 (2004-01-15):
    - added function print_string
    - Basic stack and initialization
*/


.code16         /* 16bit RealMode code */
.intel_syntax  /* I am one of the few people who HATE AT&T-style ;-) */


    /* make sure to be at 7c0h:00 instead of 00:7c00h*/
    jmp 0x07c0, offset start


    /* here we really begin to do something */
start:
    /*
     *  We definitely need a stack
     */
    cli            /* we don't have a stack in the next few nanoseconds ;-) */
    mov ax, 0x07e0      /* 0x7c0 + 512 bytes */
    mov ss, ax
    mov ax, 512     /* 512 bytes for the stack should be enough to start with */
    mov sp, ax

    mov ax, 0x07c0  /* and by the way, update DS */
    mov ds, ax
    sti            /* now we have a proper stack, no problems for interrupts */    /*
     *  the stack is now just behind the bootsector
     */

    /* ensure to be in the correct graphics mode */
    mov ax, 0x0003
    int 0x10



    /*
     *  and clear the screen, just in case the BIOS behaves odd
     */
clear_scr:
    /* NOTE: we assume non-MDA and non-HGA! */
    mov ax, 0xb800
    mov es, ax
    mov bp, 0x00
    mov cx, 2000
    lp_clrscr:
        mov ax, 0x0700  /* color, character "0" */
        mov es:[bp], ax /* output goes to page 0, color */
        add bp, 2
    loop lp_clrscr

    /* and move to the top left corner */
    mov ax, 0x0200
    mov bx, 0x0000
    mov dx, 0x0000
    int 0x010

    /*
     *  Now, the screen and everything is initialized and we can start
     *  to work.
     */

load_stage2:
    /* first, print message saying what we're doing now */
    mov si, offset msg_load
    call print_string

    /* get the boot drive */


    jmp halt /* stop here */


/*
 *  print_string puts a string to the screen at the current
 *  cursor's position.
 *  The string must be terminated with a zero-byte (C-style string)
 *  and is interpreted by the BIOS according to BELL standards.
 *  The string is expected to be at DS:SI.
 *
 *  NOTE: After a call to print_string, SI points to the end of the string!!!
 */
print_string:
    push ax        # safe the used registers
    push bx

    mov ah, 0x0e
    mov bx, word ptr 0x0000
    print_loop:
        mov al, byte ptr [si]
        cmp al, 0x00
        jz ps_halt
        int 0x10
        inc di
    jmp print_loop

    ps_halt:
    pop bx
    pop ax
    ret



halt:
    cli
    HLT




/*
 *  Data section
 *  All variables and static data (mainly messages) goes here
 */
msg_load:
    .ascii "Loading stage 2...\x0d\x0a\x00"


/*
 *  Pad the binary with security code (normally we should never jump there)
 *  and append boot signature
 */
.fill (510 - .)/2, 2, 0xF4F4
.byte 0x55, 0xAA
Zu kompilieren mit dem GNU Assembler (GAS) und zu Speichern im ersten Sektor einer beliebigen Diskette und bootbar mit jedem IBM-kompatiblen PC-BIOS.
War mal als kleiner Bootloader zum Rumspielen gedacht (daher am Ende die eigenartigen Kommentare), läuft nur bis er den String ausgibt. Zwar ein wenig komplexer aufgebaut als Marcs Beispiel, aber dafür muss man sich um die Länge des Strings keine Sorgen machen (solange er nicht länger ist als 510-(assemblierter Programmcode) Byte. Um es noch weiter zurechtzustutzen, damit wirklich nur der String ausgegeben wird, bin ich jetzt zu faul.
  Mit Zitat antworten Zitat