Computer Architecture and Assembly Language [ Irvine32.inc ]

Assembly Language Program taking user input and displaying on screen.

section .bss ;data
 num1 resb 5
 num2 resb 5

section .data
 msg db ' Elementary Arithmetic', 0xa ;string to be printed
 len equ $ - msg ;length of the string

 breakLine db " ", 0xa
 lenBreakLine equ $ - breakLine

 msg2 db "Enter 2 numbers, and I'll show you the sum, difference, product, quotient, and remainder.", 0xa
 len2 equ $ - msg2

 firstnum db "First number:", 0xa
 lenfirstnum equ $ - firstnum

 secnum db "Second number:", 0xa
 lensecnum equ $ - secnum

section .text
 global _start 

 ; OUT PUT 
 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

; BREAK THE LINE
 mov edx,lenBreakLine ;BreakLine length
 mov ecx,breakLine ;message to write
 mov ebx,1 ;file descriptor (stdout)
 mov eax,4 ;system call number (sys_write)
 int 0x80 ;call kernel

; OUT PUT THE INSTRUCTIONS
 mov edx,len2 ;message length
 mov ecx,msg2 ;message to write
 mov ebx,1 ;file descriptor (stdout)
 mov eax,4 ;system call number (sys_write)
 int 0x80 ;call kernel

; BREAK THE LINE
 mov edx,lenBreakLine ;BreakLine length
 mov ecx,breakLine ;message to write
 mov ebx,1 ;file descriptor (stdout)
 mov eax,4 ;system call number (sys_write)
 int 0x80 ;call kernel

; Enter First Number Message
 mov edx,lenfirstnum ;BreakLine length
 mov ecx,firstnum ;message to write
 mov ebx,1 ;file descriptor (stdout)
 mov eax,4 ;system call number (sys_write)
 int 0x80 ;call kernel

;Read and store the user input First
 mov eax, 3
 mov ebx, 2
 mov ecx, num1
 mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
 int 80h

; Enter Second Number Message
 mov edx,lensecnum ;BreakLine length
 mov ecx,secnum ;message to write
 mov ebx,1 ;file descriptor (stdout)
 mov eax,4 ;system call number (sys_write)
 int 0x80 ;call kernel

;Read and store the user input Second
 mov eax, 3
 mov ebx, 2
 mov ecx, num2
 mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
 int 80h

;Output the number entered
 mov eax, 4
 mov ebx, 1
 mov ecx, num1
 mov edx, 5
 int 80h

mov eax,1 ;system call number (sys_exit)
 int 0x80 ;call kernel

 

Q. Write program in assembly language Using Irvine32, Perform all arithmetic operations on 2 numbers ( entered by the user).

INCLUDE Irvine32.inc
.data
 intro_1 BYTE " Elementary Arithmetic", 0
 intro_2 BYTE "Enter 2 numbers, I'll show you the sum, difference, product, quotient, and remainder. ",0
 prompt_1 BYTE "First number: ", 0
 prompt_2 BYTE "Second number: ", 0
 prompt_3 BYTE " remainder "

 num1 DWORD ? ;integer to be entered by user
 num2 DWORD ?
 res1 DWORD ?
 res2 DWORD ?
 res3 DWORD ?
 res4 DWORD ?
 remainder DWORD ?

goodBye BYTE "Ok Bye!, ", 0

.code
 main PROC

;Introduce programmer
 mov edx, OFFSET intro_1
 call WriteString
 call CrLf

;Display Message of program
 mov edx, OFFSET intro_2
 call WriteString
 call CrLf
 call CrLf

;Get First Number
 mov edx, OFFSET prompt_1
 call WriteString
 call ReadInt
 mov num1, eax
 call CrLf

;Get Second Number
 mov edx, OFFSET prompt_2
 call WriteString
 call ReadInt
 mov num2, eax
 call CrLf

;addition Calculate
 mov eax, num1
 mov ebx, num2
 add eax, ebx
 mov res1, eax

;Subtraction Calculate
 mov eax, num1
 mov ebx, num2
 sub eax, ebx
 mov res2, eax

;Multiplication Calculate
 mov eax, num1
 mov ebx, num2
 mul ebx
 mov res3, eax

;Multiplication Calculate
 mov eax, num1
 mov ebx, num2
 div ebx
 mov res4, eax
 mov remainder, edx

;Report result Addition
 mov eax, num1
 call WriteDec
 mov al,'+'
 call WriteChar
 mov eax, num2
 call WriteDec
 mov al,'='
 call WriteChar
 mov eax, res1
 call WriteDec
 call CrLf

;Report result subtraction
 mov eax, num1
 call WriteDec
 mov al,'-'
 call WriteChar
 mov eax, num2
 call WriteDec
 mov al,'='
 call WriteChar
 mov eax, res2
 call WriteDec
 call CrLf

;Report result Multiplication
 mov eax, num1
 call WriteDec
 mov al,'x'
 call WriteChar
 mov eax, num2
 call WriteDec
 mov al,'='
 call WriteChar
 mov eax, res3
 call WriteDec
 call CrLf

;Report result Division
 mov eax, num1
 call WriteDec
 mov al,'%'
 call WriteChar
 mov eax, num2
 call WriteDec
 mov al,'='
 call WriteChar
 mov eax, res4
 call WriteDec
 mov edx, OFFSET prompt_3
 call WriteString
 mov eax, remainder
 call WriteDec

call CrLf
 call CrLf

;Say "Good-bye"
 mov edx, OFFSET goodBye
 call WriteString
 call CrLf

exit ; exit to operating system
 main ENDP

END main

 

Leave a Reply