Write assembly language code for displays the following string
on the screen
Hello,
programmers!
Welcome
to the world of,
Linux assembly
programming!
section
.text
global _start ;must be
declared for using gcc
_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, programmers!',0xa
db 'Welcome to the world of,',0xa
db 'Linux assembly programming!',0xa
db 'TCEUSL',0xa ;our dear string
len equ $ - msg
;length of our dear string
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2. Write assembly language code for Displays 9
stars on the screen along with a simple message
section .text
global _start ;must be declared for linker
(gcc)
_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 edx,12 ;message length
mov ecx,s2 ;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 'Displaying 12 stars',0xa ;a message
len equ $ - msg ;length of message
s2 times 12 db '*'
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3. Write assembly language code for reads a
number from the keyboard and displays it on the screen
section .data
;Data segment
userMsg db 'Please enter a number: ' ;Ask the user to enter a
number
lenUserMsg equ
$-userMsg
;The length of the message
dispMsg db 'You have entered: '
lenDispMsg equ
$-dispMsg
section .bss
;Uninitialized data
num resb 5
section .text ;Code
Segment
global _start
_start:
;User prompt
mov eax, 4
mov ebx, 1
mov ecx, userMsg
mov edx, lenUserMsg
int 80h
;Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 5
;5 bytes (numeric, 1 for sign) of that information
int 80h
;Output the message 'The entered number is: '
mov eax, 4
mov ebx, 1
mov ecx, dispMsg
mov edx, lenDispMsg
int 80h
;Output the number entered
mov eax, 4
mov ebx, 1
mov ecx, num
mov edx, 5
int 80h
;assigning new value
mov eax, 4
mov ebx, 1
mov ecx, userMsg
mov edx, lenUserMsg
int 80h
;Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 5
;5 bytes (numeric, 1 for sign) of that information
int 80h
;Output the message 'The entered number is: '
mov eax, 4
mov ebx, 1
mov ecx, dispMsg
mov edx, lenDispMsg
int 80h
;Output the number entered
mov eax, 4
mov ebx, 1
mov ecx, num
mov edx, 5
int 80h
; Exit code
mov eax, 1
mov ebx, 0
int 80h
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
4. Write assembly language code for stores a name 'Zara Ali' in the
data section of the memory. Then changes its value to another name 'Nuha Ali'
and displays both the names.
section
.text
global_start ;must be declared for linker
(ld)
_start:
;tell linker entry point
;writing the name 'Zara Ali'
mov edx,9
;message length
mov ecx, name ;message to write
mov ebx,1
;file descriptor (stdout)
mov eax,4
;system call number (sys_write)
int
0x80 ;call kernel
mov [name], dword 'Nuha'
; Changed the name to Nuha Ali
;writing the name 'Nuha Ali'
mov edx,8
;message length
mov ecx,name ;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
name db 'Zara Ali '
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
5. Write assembly language code for asks two digits from the user,
stores the digits in the EAX and EBX register respectively, adds the values,
stores the result in a memory location 'res' and finally displays the
result.
Enter a digit:
3
Please enter a second
digit:
4
The sum is:
7
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
segment .data
msg1 db "Enter a digit ", 0xA,0xD
len1 equ $- msg1
msg2 db "Please enter a second digit", 0xA,0xD
len2 equ $- msg2
msg3 db "The sum is: "
len3 equ $- msg3
segment .bss
num1 resb 2
num2 resb 2
res resb 1
section .text
global _start ;must be declared for using gcc
_start:
;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int
0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num1
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num2
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0x80
; moving the first number to eax register and second number to ebx
; and subtracting ascii '0' to convert it into a decimal number
mov eax, [num1]
sub eax, '0'
mov ebx, [num2]
sub ebx, '0'
; add eax and ebx
add eax, ebx
; add '0' to to convert the sum from decimal to ASCII
add eax, '0'
; storing the sum in memory location res
mov [res], eax
; print the sum
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, res
mov edx, 1
int 0x80
exit:
mov eax, SYS_EXIT
xor ebx, ebx
int 0x80
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
6. Write assembly language code for multiplies 3 with 2, and
displays the result
section
.text
global _start ;must be declared for using gcc
_start:
;tell linker entry point
mov al,'3'
sub al, '0'
mov bl, '2'
sub bl, '0'
mul bl
add al, '0'
mov [res], al
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor
(stdout)
mov eax,4 ;system call number
(sys_write)
int 0x80 ;call kernel
mov ecx,res
mov edx, 1
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 "The result is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
7. Write assembly language code for divides 8 with 2. The dividend
8 is stored in the 16 bit AX register and the divisor 2 is stored in the 8 bit
BL register.
section
.text
global _start ;must be declared for using gcc
_start:
;tell linker entry point
mov ax,'8'
sub ax, '0'
mov bl, '2'
sub bl, '0'
div bl
add ax, '0'
mov [res], ax
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor
(stdout)
mov eax,4 ;system call number
(sys_write)
int 0x80 ;call kernel
mov ecx,res
mov edx, 1
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 "The result is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
8. Write assembly language code for check whether a given number is
odd or even
section
.text
global
_start ;must
be declared for using gcc
_start:
;tell linker entry point
mov ax, 8h
;getting 8 in the ax
and ax,
1
;and ax with 1
jz evnn
mov eax,
4
;system call number (sys_write)
mov ebx,
1 ;file
descriptor (stdout)
mov ecx, odd_msg
;message to write
mov edx,
len2 ;length of message
int
0x80
;call kernel
jmp outprog
evnn:
mov ah, 09h
mov eax,
4
;system call number (sys_write)
mov ebx,
1 ;file
descriptor (stdout)
mov ecx, even_msg
;message to write
mov edx,
len1 ;length of message
int
0x80
;call kernel
outprog:
mov
eax,1
;system call number (sys_exit)
int
0x80
;call kernel
section .data
even_msg db 'Even Number!' ;message showing even number
len1 equ $ - even_msg
odd_msg db 'Odd Number!' ;message showing odd number
len2 equ $ - odd_msg
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
9. Write assembly language code for displays the largest of three
variables. The variables are double-digit variables. The three variables num1,
num2 and num3 have values 47, 72 and 31 respectively.
section
.text
global _start
;must be declared for using gcc
_start:
;tell linker entry point
mov ecx, [num1]
cmp ecx, [num2]
jg check_third_num
mov ecx, [num2]
check_third_num:
cmp ecx, [num3]
jg _exit
mov ecx, [num3]
_exit:
mov [largest], ecx
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number
(sys_write)
int 0x80 ;call kernel
mov ecx,largest
mov edx, 2
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number
(sys_write)
int 0x80 ;call kernel
mov eax, 1
int 80h
section .data
msg db "The largest digit is: ", 0xA,0xD
len equ $- msg
num1 dd '47'
num2 dd '22'
num3 dd '31'
segment .bss
largest resb 2
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
10. Write
assembly language code for prints the number 1 to 9 on the screen:
section
.text
global _start ;must be
declared for using gcc
_start:
;tell linker entry point
mov ecx,10
mov eax, '1'
l1:
mov [num], eax
mov eax, 4
mov ebx, 1
push ecx
mov ecx, num
mov edx, 1
int 0x80
mov eax, [num]
sub eax, '0'
inc eax
add eax, '0'
pop ecx
loop l1
mov
eax,1
;system call number (sys_exit)
int
0x80
;call kernel
section .bss
num resb 1
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
11. Write
assembly language code for adds up two 5-digit decimal numbers and displays the
sum.
section
.text
global _start ;must be
declared for using gcc
_start:
;tell linker entry point
mov ecx,10
mov eax, '1'
l1:
mov [num], eax
mov eax, 4
mov ebx, 1
push ecx
mov ecx, num
mov edx, 1
int 0x80
mov eax, [num]
sub eax, '0'
inc eax
add eax, '0'
pop ecx
loop l1
mov
eax,1
;system call number (sys_exit)
int
0x80
;call kernel
section .bss
num resb 1
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
12. Write
assembly language code for defining a 3 element array x, which stores three
values. 2, 3 and 4. It adds the values in the array and displays the sum 9.
section
.text
global _start ;must be declared for linker (ld)
_start:
mov eax,3 ;number bytes to be
summed
mov ebx,0 ;EBX will store the
sum
mov ecx, x ;ECX will point to the
current element to be summed
top: add ebx, [ecx]
add ecx,1 ;move pointer to
next element
dec eax ;decrement
counter
jnz top ;if
counter not 0, then loop again
done:
add ebx, '0'
mov [sum], ebx ;done, store result in "sum"
display:
mov edx,1 ;message length
mov ecx, sum ;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
global x
x:
db 2
db 4
db 3
sum:
db 0
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
13. Write
assembly language code for calculating factorial to any given number.
section
.text
global _start
;must be declared for using gcc
_start:
;tell linker entry point
mov bx,
3 ;for
calculating factorial 3
call proc_fact
add ax, 30h
mov [fact], ax
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
edx,1
;message length
mov
ecx,fact ;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
proc_fact:
cmp bl, 1
jg do_calculation
mov ax, 1
ret
do_calculation:
dec bl
call proc_fact
inc bl
mul bl ;ax =
al * bl
ret
section .data
msg db 'Factorial 3 is:',0xa
len equ $ - msg
section .bss
fact resb 1
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
14. Write
assembly language code for Creates and open a file named myfile.txt, and writes
a text 'Welcome to Tutorials Point' in this file. Then program reads from the
file and stores the data into a buffer named info. Displays the text as stored
in info.
section .text
global _start
;must be declared for using gcc
_start:
;tell linker entry point
;create the file
mov eax, 8
mov ebx, file_name
mov ecx, 0777
;read, write and execute by all
int
0x80
;call kernel
mov [fd_out], eax
; write into the file
mov
edx,len ;number of
bytes
mov ecx,
msg ;message to write
mov ebx, [fd_out] ;file
descriptor
mov
eax,4
;system call number (sys_write)
int
0x80
;call kernel
; close the file
mov eax, 6
mov ebx, [fd_out]
; write the message indicating end of file write
mov eax, 4
mov ebx, 1
mov ecx, msg_done
mov edx, len_done
int 0x80
;open the file for reading
mov eax, 5
mov ebx, file_name
mov ecx,
0 ;for
read only access
mov edx,
0777 ;read, write and
execute by all
int 0x80
mov [fd_in], eax
;read from file
mov eax, 3
mov ebx, [fd_in]
mov ecx, info
mov edx, 26
int 0x80
; close the file
mov eax, 6
mov ebx, [fd_in]
int 0x80
; print the info
mov eax, 4
mov ebx, 1
mov ecx, info
mov edx, 26
int 0x80
mov
eax,1
;system call number (sys_exit)
int
0x80
;call kernel
section .data
file_name db 'myfile.txt'
msg db 'Welcome to Tutorials Point'
len equ $-msg
msg_done db 'Written to file', 0xa
len_done equ $-msg_done
section .bss
fd_out resb 1
fd_in resb 1
info resb 26
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
15. Using assembly
language, write a program to prints descending rows of stars using loops.
Example
of output:
a. *********
*********
*********
*********
*********
*********
*********
*********
***********
b. *********
********
*******
******
*****
****
***
**
*
global _start
section .data
rows dw 10
section .text
_start:
;it defines how many symbols we have to print
movzx ebx, byte[rows] ; ebx holds number of rows
lea eax,[ebx+3]
imul eax,ebx
shr eax,1 ; now eax holds number of all symbols
mov edx,eax ; now edx holds number of all symbols, used in print
;prepare pointer
mov ecx,esp
sub ecx,eax ; ecx points on the beginning of the string, used in print
;fill the string by stars
mov eax,edx
shr eax,2
mov ebp, dword '****'
next_star:
mov [ecx+4*eax],ebp
dec eax
jge next_star
;fill the string by '\n'
mov edi,esp
dec edi
mov eax,ebx; in the eax is number of rows
inc eax
next_n:
mov [edi],byte 0xa
sub edi,eax
dec eax
jg next_n
;print
;mov ecx,esp
mov eax,4; ;sys_write
mov ebx,1; ;1 - stdout
int 80h;
;exit
mov eax,1 ;1 - sys_exit
xor ebx,ebx ;0 - return 0
int 80h;
ret
16.add sub
section .data
nextline: db 0xa
section .bss
num1 resb 2
num2 resb 2
sum resb 2
subt resb 2
mult resb 2
divt resb 2
section .text
global _start:
_start:
mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 2
int 80h
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 2
int 80h
mov eax, [num1]
sub eax, '0'
mov ebx, [num2]
sub ebx, '0'
add eax, ebx
add eax, '0'
mov [sum], eax
mov eax, 4
mov ebx, 1
mov ecx, sum
mov edx, 2
int 80h
mov eax, 4
mov ebx, 1
mov ecx, nextline
mov edx, 1
int 80h
mov eax, [num1]
sub eax, '0'
mov ebx, [num2]
sub ebx, '0'
sub eax, ebx
add eax, '0'
mov [subt], eax
mov eax, 4
mov ebx, 1
mov ecx, subt
mov edx, 2
int 80h
mov eax, 4
mov ebx, 1
mov ecx, nextline
mov edx, 1
int 80h
mov ax, 0
mov al, [num1]
sub al, '0'
mov bl, [num2]
sub bl, '0'
mul bl
add ax, '0'
mov [mult], ax
mov eax, 4
mov ebx, 1
mov ecx, mult
mov edx, 2
int 80h
mov eax, 4
mov ebx, 1
mov ecx, nextline
mov edx, 1
int 80h
mov ax, 0
mov dx, 0
mov al, [num1]
sub al, '0'
mov bl, [num2]
sub bl, '0'
div bl
add ax, '0'
mov [divt], ax
mov eax, 4
mov ebx, 1
mov ecx, divt
mov edx, 2
int 80h
mov eax, 1
mov ebx, 0
int 80h