Write an SRC assembly code to work both multiplication (*) and division (/). Ope
ID: 3565096 • Letter: W
Question
Write an SRC assembly code to work both multiplication (*) and division (/). Operands are defined as constants, and the operator is defined as an ASCII character as follows: Character ASCII * 42 / 47 op1 * op2 OR op1 / op2 Your program must read first operand, the operator, and then the second operand as above. Store the result in the memory location for result. When the operation is division (/), the result is the integer part of the division (e.g., 9/4 = 2). If the second operand in division is zero, store -1 in the result. You may start with the following template.
;
; Multiplication and division (integer division) using addition and
; subtraction in SRC assembly language.
; If the second operand in division is zero, store -1 in the result.
;
.org 1000
ld r1, op1 ; r1 holds operand1
ld r2, op2 ; r2 holds operand2
ld r3, operator ; r3 holds the operator
lar r4, result ; r4 points to the location of result
lar r8, end
lar r10, multiplication
lar r12, division
addi r5,r3,-42 ; check for multiplication operation
brzr r10,r5 ; if yes, branch to multiplication
addi r5,r3,-47 ; check for division operation
brzr r12,r5 ; if yes, branch to division
br r8
multiplication:
lar r13, aa
lar r14, bb
lar r15, loop1
lar r16, store
la r20, 0 ; r20: number of negative values in op1 and op2
brpl r13,r1 ; check the sign of the first operand
neg r1,r1 ; if the sign is negative, make it to be positive
addi r20,r20,1 ;
aa:
; DEFINE YOUR CODE ; check the sign of the second operand
; DEFINE YOUR CODE ; if the sign is negative, make it to be positive
;DEFINE YOUR CODE
bb:
addi r22,r2, 0 ;save the second operand as counter
la r6, 0
loop1:
add ; DEFINE YOUR CODE
addi ; DEFINE YOUR CODE ; decrement the value of r22
; DEFINE YOUR CODE ; if r22's value is nonzero, go to loop1
addi r20, r20, -1 ; r20's possible value was 0, 1, or 2
brnz r16,r20 ; if r20 = 0,
neg ; DEFINE YOUR CODE ; one operand was negative and the other was positive
store:
st ; DEFINE YOUR CODE
br ; DEFINE YOUR CODE
division:
;
; CONTINUE YOUR CODING FOR DIVISION
;
end: stop ; exit
.org 3000
op1: .dc 20 ; it can be any integer value.
operator: .dc "*" ; it can be either '*' or '/'
op2: .dc -7 ; it can be any integer value.
result: .dw 1 ; storage for result
;
; end of the program
;
Explanation / Answer
Look at the following simple program to understand the use of registers in assembly programming. This program displays 9 stars on the screen along with a simple message:
When the above code is compiled and executed, it produces the following result:
Assembly - System Calls
System calls are APIs for the interface between user space and kernel space. We have already used the system calls sys_write and sys_exit for writing into the screen and exiting from the program, respectively.
Linux System Calls
You can make use of Linux system calls in your assembly programs. You need to take the following steps for using Linux system calls in your program:
Put the system call number in the EAX register.
Store the arguments to the system call in the registers EBX, ECX, etc.
Call the relevant interrupt (80h).
The result is usually returned in the EAX register.
There are six registers that store the arguments of the system call used. These are the EBX, ECX, EDX, ESI, EDI, and EBP. These registers take the consecutive arguments, starting with the EBX register. If there are more than six arguments, then the memory location of the first argument is stored in the EBX register.
The following code snippet shows the use of the system call sys_exit:
The following code snippet shows the use of the system call sys_write:
All the syscalls are listed in /usr/include/asm/unistd.h, together with their numbers (the value to put in EAX before you call int 80h).
The following table shows some of the system calls used in this tutorial:
Example
%eax Name %ebx %ecx %edx %esx %edi 1 sys_exit int - - - - 2 sys_fork struct pt_regs - - - - 3 sys_read unsigned int char * size_t - - 4 sys_write unsigned int const char * size_t - - 5 sys_open const char * int int - - 6 sys_close unsigned int - - - -Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.