Hello World function without using C printf

UPDATED

This is my second day working with NASM. After a thorough understanding of this

section .programFlow
    global _start
    _start:
        mov edx,len
        mov ecx,msg
        mov ebx,0x1    ;select STDOUT stream
        mov eax,0x4    ;select SYS_WRITE call
        int 0x80       ;invoke SYS_WRITE
        mov ebx,0x0    ;select EXIT_CODE_0
        mov eax,0x1    ;select SYS_EXIT call
        int 0x80       ;invoke SYS_EXIT
section .programData
    msg: db "Hello World!",0xa
    len: equ $ - msg

I would like to wrap this stuff inside an assembly function. All (or most) examples on the Internet use externand call a printffunction C(see code below) - and I do not want this. I want to learn how to create the "Hello World" function in an assembly without using C printf(or even other calls to external functions).

global  _main
extern  _printf

section .text
    _main:
        push    message
        call    _printf
        add     esp, 4
        ret
section .data
    message: db "Hello, World", 10, 0

Update

I am building for Linux, but since I don't have a Linux box, I run my build code here compile_assembly_online .

+4
3

int 0x80 Windows DOS , Linux. , , .

Windows, - Windows API, ( ) WriteConsole(). C .

"", , int 0x80 , , , Linux, Windows DOS.

DOS, - Ralf Brown, Int21/Fn9.

+2

, Windows, :

DOS, , DOS:

int 21, 9 : AH 9, DS: DX , $, .

int 21, 2 , , $ ( Ctrl + C ). AH 2, DL ASCII ( ) .

+2

, Nasm "" - ".text", ".data" ".bss" ( , ). "." . , , "", "", . , .programData is going to be read-only. Since you don't try to write to it this isn't going to do any harm... but.data` .

asm Linux, , . , - . - "andlinux" ( ), Linux- Windows. Linux " ". Linux.

DOS DosBox... " DOS" . " " B800h: xxxx. ( "character" "color" ). " ", , . . !

, , , . "msg" "len", , - . , - , . , (printf does, sys_write ) , edx. , int 80h vs int 21h vs WriteFile. , ...

EDIT: , . , call ( ) , ret , , ss:sp . , , , ret.

; purpose: to demonstrate a subroutine
; assemble with: nasm -f bin -o myfile.com myfile.asm
; (for DOS)

; Nasm defaults to 16-bit code in "-f bin" mode
; but it won't hurt to make it clear
bits 16 

; this does not "cause" our code to be loaded
; at 100h (256 decimal), but informs Nasm that
; this is where DOS will load a .com file
; (needed to calculate the address of "msg", etc.)
org 100h

; we can put our data after the code
; or we can jump over it
; we do not want to execute it!
; this will cause Nasm to move it after the code
section .data
    msg db "Hello, World!", 13, 10, "$"
    msg2 db "Goodbye cruel world!", 13, 10, "$"

section .text
; execution starts here
    mov dx, msg ; address/offset of msg
    call myprint
; "ret" comes back here
; no point in a subroutine if we're only going to do it once
    mov dx, msg2
    call myprint
; when we get back, do something intelligent

exit:
    mov ah. 4Ch ; DOS exit subfunction
    int 21h
; ---------------------------------------

; subroutines go here, after the exit
; we don't want to "fall through" into 'em!
myprint:
; expects: address of a $-terminated string in dx
; returns: nothing
    push ax ; don't really need to do this
    mov ah, 9 ; DOS print subfunction
    int 21h ; do it
    pop ax ; restore caller ax - and our stack!
    ret
; end of subroutine

, , "". - , dx. Linux ( ). ...

+1

Source: https://habr.com/ru/post/1530819/


All Articles