Skip to content

Commit

Permalink
Merge pull request #5 from Sumukha87/main-1
Browse files Browse the repository at this point in the history
Create helloWorld.asm
  • Loading branch information
Samsonroyal authored Jun 11, 2023
2 parents c9c959c + 0be3cb3 commit b17d36f
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions helloWorld.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
; Prints "hello world" to the screen.
; OS/X requires system call arguments to be pushed onto the stack in reversed
; order, with an extra 4 bytes (DWORD) at the end.
;
; Build with:
; nasm -f macho hello.asm
; ld -o hello hello.o
;
; Run with:
; ./hello
;
; Author: Sumukha87



SECTION .data ; initialised data section

Msg: db "hello world", 10 ; message to print
MsgLen: equ $ - Msg ; length of message


SECTION .text ; code section

global start
start:

; printing message, use write()
; system call 4 syntax:
; user_ssize_t write(int fd, user_addr_t cbuf, user_size_t nbyte)
push dword MsgLen ; length of message to print
push dword Msg ; message to print
push dword 1 ; FD of 1 for standard output
sub esp, 4 ; OS/X requires extra 4 bytes after arguments
mov eax, 4 ; 4 - write() system call
int 80H ; perform system call
add esp, 16 ; restore stack (16 bytes pushed: 3 * dword + 4)

; program exit, use sys_exit()
push dword 0 ; exit value of 0 returned to the OS
sub esp, 4 ; OS/X requires extra 4 bytes after arguments
mov eax, 1 ; 1 - sys_exit() system call
int 80H ; perform system call
; no need to restore stack, code after this line will not be executed
; (program exit)

@Sumukha87

0 comments on commit b17d36f

Please sign in to comment.