Pejman Moghadam / Assembly

Linux Assembly - Welcome Message

Public domain


; This program after asking the name of user
; will say welcome to him/her.
; Filename: welcome.asm
; Compile : nasm -f elf welcome.asm -l welcome.lst
; Link    : ld -s -o welcome welcome.o
section .data
nwln     db      0xa,0   ;new line
dot      db      ".",0   ;single dot
prompt   db      "What is your name ? ",0
message  db      "Welcome ",0

section .bss
name     resb    32      ;user's name

section .text
global _start
_start:

;write prompt to stdout
        mov     eax,prompt
        call    print_string

;read user's name from stdin
        mov     eax,name      ;buffer
        mov     ebx,32        ;length
        call    input_string

;write message to stdout
        mov     eax,message
        call    print_string

;write user's name to stdout
        mov     eax,name
        call    print_string

;write a dot and newline to stdout
        mov     eax,dot
        call    print_string
        mov     eax,nwln
        call    print_string

;and exit
        mov     ebx,0   ;first syscall argument: exit code
        mov     eax,1   ;system call number (sys_exit)
        int     0x80    ;call kernel

;procedure : write a string to stdout
; input parameters :
;       eax must point to string
;       string must end with 0
; syscall : /usr/include/asm-i386/unistd.h
;       #define __NR_write 4
;       eax = 4
; man 2 write :
;       ssize_t write(int fd, const void *buf, size_t count);
;       ebx=(int fd), ecx=(const void *buf), edx=(size_t count)
print_string :
        mov     ecx,eax ;second argument: pointer to message
        mov     edx,0   ;third argument:  message length
cal_str_len:            ;calculate string length
        inc     edx            ;start from second char
        mov     ah,[ecx+edx]   ;put char in ah
        cmp     ah,0           ;compare char with 0
        jne     cal_str_len    ;if not zero check next char
                               ;until find zero, then length
                               ;will be in edx
        mov     ebx,1   ;first argument:  file handle (stdout)
        mov     eax,4   ;system call number (sys_write)
        int     0x80    ;call kernel
        ret

;procedure : read a string from stdin
; input parameters :
;       eax must point to buffer
;       ebx must set to length of buffer
; syscall : /usr/include/asm-i386/unistd.h
;       #define __NR_read 3
;       eax = 3
; man 2 read :
;       ssize_t read(int fd, void *buf, size_t count);
;       ebx=(int fd), ecx=(void *buf), edx=(size_t count)
input_string :
        mov     edx,ebx   ;third argument: buffer length
        mov     ecx,eax   ;second argument: pointer to buffer
        mov     ebx,1     ;first argument: file handle (stdin)
        mov     eax,3     ;system call number (sys_read)
        int     0x80      ;call kernel

                          ;finding end of string
                          ;and put zero there
        mov     edx,0     ;init counter for str chars
find_crlf:
        inc     edx       ;start from second char
        mov     ah,0xa    ;we will check 'line feed'
        cmp     ah,[ecx+edx] ;compare 'line feed' with char
        je      set_zero  ;if they equal so this is end of str
        mov     ah,0xd    ;next check 'carriage return'
        cmp     ah,[ecx+edx] ;compare 'carriage return' with char
        je      set_zero  ;if they equal so this is end of str
        jmp     find_crlf ;check next char
                          ;let's set zero instead of
set_zero:                 ;'carriage return' or 'line feed'
        mov     bh,0      ;put zero in bh
        add     ecx,edx   ;make ecx point to the end of str
        mov     [ecx],bh  ;put zero at the end
        ret               ;exit procedure

BY: Pejman Moghadam
TAG: asm, hello-world
DATE: 2008-08-09 01:06:50


Pejman Moghadam / Assembly [ TXT ]