Assembly Language Program to Prompt a user to write his name and Greet Him with Welcome followed by his name


Here we are going to write a simple assembly language program to ask an user to type his name and display his name with Welcome greetings.

Sir is told us not to use printf statement of gcc multillib so we are not going to use this printf in this example.

Here is the link of the code  https://gist.github.com/1594788

section .data
; Declare/store the information “Hello World!”
prompt db ‘What is your name? ‘
; do not change the order of the following three lines!
helloMsg dq ‘Hello ‘
name db ‘ ‘ ; space characters
endOfLine db ‘!’
; do not change the order of the previous three lines!
section .text
global _start
_start:
; Output the information ‘What is your name? ‘
mov eax, 4 ; write…
mov ebx, 1 ; to the standard output (screen/console)…
mov ecx, prompt ; the information at memory address prompt
mov edx, 19 ; 19 bytes (characters) of that information
int 0x80 ; invoke an interrupt
; Accept input and store the user’s name
mov eax, 3 ; read…
mov ebx, 1 ; from the standard input (keyboard/console)…
mov ecx, name ; storing at memory location name…
mov edx, 23 ; 23 bytes (characters) is ok for my name
int 0x80
; Output that information “Hello…”
mov eax, 4 ; write…
mov ebx, 1 ; to the standard output (screen/console)…
mov ecx, helloMsg ; the information at helloMsg…
mov edx, 23 ; 23 bytes (characters) is ok for my name
int 0x80
; Exit
mov eax, 1 ; sys_exit
mov ebx, 0 ; exit status. 0 means “normal”, while 1 means “error”
int 0x80
To compile and run this program
$nasm -f elf greet.asm
$ld -s -o greet greet.o
$./greet
What is your name? Sanjib Narzary //this is input
Hello Sanjib Narzary //this is ouput
$
Thats all. Now your task is to ask user to type two numbers and add them and display the result. 🙂
Advertisements

Character Inputting and Outputting Assembly Program


Here is a simple Assembly Code for Character Inputting from standard device and Outputting to standard output. https://gist.github.com/1594520

section .data
;New line string
NEWLINE: db 0xa, 0xd
LENGTH: equ $-NEWLINE
section .bss
INPT: resd 1
section .text
global _start
_start:
;Read character
mov eax, 0x3
mov ebx, 0x1
mov ecx, INPT
mov edx, 0x1
int 80h
;print character
mov eax, 0x4
mov ebx, 0x1
mov ecx, INPT
mov edx, 0x1
int 80h
;Print new line after the output
mov eax, 0x4
mov ebx, 0x1
mov ecx, NEWLINE
mov edx, LENGTH
int 0x80
;Terminate
mov eax, 0x1
xor ebx, ebx
int 0x80
Compile by typing
$nasm -f elf cio.asm
$ld -s -o cio cio.o
$./cio
a //this is input
a //this is output
$

Testing of a Loop in Assembly Language Using nasm in Ubuntu Linux


Assembly Language program to count 10 tuimes

https://gist.github.com/1593466

section .text
global main
extern printf
main:
mov ebx,10 ;number 10 to ebx
loop: push ebx ;first parameter
push message ;second parameter
call printf ;call inbuilt printf statement for outputting
dec ebx ;decrement the value of ebx by 1
jnz loop ;if not equal to zero go to loop
add esp,80 ;clear all the stack
ret
message db “Value = %d”,10,0
To run this program
$ nasm -f elf loop.asm
$gcc -m32 -o loop loop.o
//for this you need to have a gcc multilib just install by typing #sudo apt-get install gcc-multilib
$./loop
Value = 10
Value = 9
Value = 8
Value = 7
Value = 6
Value = 5
Value = 4
Value = 3
Value = 2
Value = 1
$
You are finished. Now your task is to print 0 to 10;

Adding two numbers in Assembly Language Linux (Ubuntu) using nasm


Intel X86 32 bit programming Assembly Language.

Problem: Add two numbers 45 and 55 and display the content in Console/Terminal output.

Soloution:

To solve that problem we will load number 45 in eax register and number 55 in ebx register and add the two and store in eax. And print the content of register eax to standard output.

Here is the program addition.asm
https://gist.github.com/1589234

section .text
global main
extern printf
main:
   mov eax, 45
   mov ebx, 55
   add eax,ebx
   push eax
   push message
   call printf
   add esp, 8
   ret
message db “Value = %d”, 10, 0

To run this

$nasm -f elf addition.asm

$gcc -m32 -o addition addition.o

To run this code use

$./addition

Value=100

Thats All. Now your task is to substract a number from bigger number and one more multiply two numbers.

Writing a “Hello World” Assembly Language Program in Linux (Ubuntu) Using nasm


Here in this tutorial we will write a simple program in Assembly Language that simply prints a “Hello World” in the console/terminal.

Here we will use Intel Core 2 Duo as our Machine that runs Ubuntu 11.10.

section .text
global _start ;must be declared for linker (ld)
_start: ;tell linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string

Now open the terminal by Going to Application >> Accesories >> Terminal in Ubuntu Linux

$gedit hello.asm

Copy and paste the Program in to hello.asm and Save It.

Now to compile it using Nasm

$nasm -f elf hello.asm

It should not show any error. If nasm is already not there in Ubuntu Linux then install it by typing #sudo apt-get install nasm

Now create an executable object file by typing

$ld -s -o hello hello.o

To execute the program

$./hello

Hello World

$

The above should print in the terminal. Now you are Done.

%d bloggers like this: