In: Computer Science
Write a complete ARM asembly program for following code:
g=12, h=8, i=2, j=5;
f = (g + h) - (i + j);
Your program displays the message:
f = (g + h) – (i + j) = 13
Note that 13 should be calculated, not hardcoded
Below is my current code:
.global _start
_start:
mov R0,#12
mov R1,#8
mov R2,#2
mov R3,#5
add R4,R0,R1
add R5,R2,R3
sub R6,R4,R5
How should I display the message: "f = (g + h) – (i + j) ="? And my code keeps giving me "Bad instruction" errors? How can I fix it?
Your required answer for the above problems is below -
Code -
# Program to give expression result with printed expression .data ## Data declaration section ## String to be printed: out_string: .asciiz "\nf = (g + h) - (i + j) = " .text ## Assembly language instructions go in text segment main: ## Start of code section li $t0, 12 # Initialize g = 12 li $t1, 8 # Initialize h = 8 li $t2, 2 # Initialize i = 2 li $t3, 5 # Initialize j = 5 add $t4, $t0, $t1 # Add $t4 = g + h add $t5, $t2, $t3 # Add $t5 = i + j sub $t6, $t4, $t5 # Sub $t6 = $t4 - $t5 li $v0, 4 # system call code for printing string = 4 la $a0, out_string # load address of string to be printed into $a0 syscall # call operating system to perform operation # specified in $v0 # syscall takes its arguments from $a0, $a1, ... move $a0, $t6 # load return value from $t6 to argument $a0. li $v0, 1 # load syscall print_int into $v0. syscall li $v0, 10 # terminate program syscall ## end of exp.asm
Output -
Note :- I have commented wherever it is required to make you understand the program, still you find any difficulty in understanding anything in the program then let me know. If you are happy with my response then please give positive feedback. Thanks
.global main main:
mov r0, #12
mov r1, #8
mov r2, #2
mov r3, #5
add r4, r0, r1
add r5, r2, r3
sub r6, r4, r5
ldr r7, =message
ldr r8, =len
mov r9, #1
SWI 0
.data
message:
.ascii "\nf = (g + h) - (i + j) = "
len = .-message
.end