Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could someone help me turn the BOLD code into Assmebly Code? It must start at .ORIG x3300 and be recursive (the code not BOLD cannot

Could someone help me turn the BOLD code into Assmebly Code? It must start at .ORIG x3300 and be recursive (the code not BOLD cannot be altered but is compatable with the BOLD code below.) Thank you.

#include

int charCount(char[], char);

int main() {

/* String will be no longer than 19 chars. */

char str[20], key, numChars;

printf("Enter string: ");

scanf(" %s", str);

printf("Enter a character to search: ");

scanf(" %c", &key);

numChars = charCount(str, key);

printf("'%c' occurs %d times! ", key, numChars);

return 0;

}

/**

* Recursively counts the occurrences of a character in a string.

* str - The string to search

* key - The character to search

* Returns the number of occurrences of key in str.

*/

int charCount(char str[], char key) {

int result;

if (*str == '\0') {

result = 0;

}

else {

if (*str == key) {

result = 1;

}

else {

result = 0;

}

result += charCount(str + 1, key);

}

return result;

}

______________________________________________________________________________________________________________________________

.ORIG x3000

MAIN LEA R6, MAIN ; initialize stack, pointers

ADD R5, R6, #-1

ADD R6, R6, #-10 ; push str

ADD R6, R6, #-10

ADD R6, R6, #-1 ; push key

ADD R6, R6, #-1 ; push numChars

; printf("Enter a string: ")

LEA R0, PROMPT1

PUTS

; scanf(" %s", str)

ADD R1, R5, #-15 ; set pointer R1 to beg of str

ADD R1, R1, #-4

LOOP GETC ; scan char

OUT ; echo it

ADD R2, R0, x-A ; check for newline

BRz DONE ; if newline, stop

STR R0, R1, #0 ; else, store char in str

ADD R1, R1, #1 ; increment pointer

BRnzp LOOP

DONE AND R0, R0, #0 ; append terminating null char

STR R0, R1, #0

; printf("Enter a character for which to search: ")

LEA R0, PROMPT2

PUTS

; scanf(" %c", &key)

GETC ; assume no leading whitespace, even

OUT ; if that's not precisely what scanf does

STR R0, R5, #-20 ; store char in key

; numChars = charCount(str, key)

LDR R0, R5, #-20 ; Load key into R0

ADD R6, R6, #-1 ; push key argument

STR R0, R6, #0

ADD R0, R5, #-15 ; load str into R0

ADD R0, R0, #-4

ADD R6, R6, #-1 ; push str argument

STR R0, R6, #0

LD R0, CCOUNT ; call charCount

JSRR R0

LDR R0, R6, #0 ; pop return value into R0

ADD R6, R6, #1

STR R0, R5, #-21 ; store return value in numChars

ADD R6, R6, #1 ; pop str arg

ADD R6, R6, #1 ; pop key arg

; printf("'%c' occurs %d times! ", key, numChars)

LD R0, NEWLN ; print newline

OUT

LD R0, QUOTE ; print single quote

OUT

LDR R0, R5, #-20 ; print key

OUT

LD R0, QUOTE ; print single quote

OUT

LEA R0, OCCURS ; print 'occurs'

PUTS

LDR R0, R5, #-21 ; load numChars into R0

LD R1, ASCII ; convert numChars into a char

ADD R0, R0, R1

OUT ; print numChars

LEA R0, TIMES ; print " times! "

PUTS

HALT

PROMPT1 .STRINGZ "Enter a string: "

PROMPT2 .STRINGZ "Enter a character for which to search: "

ASCII .FILL x30

NEWLN .FILL x0A

QUOTE .FILL x27

OCCURS .STRINGZ " occurs "

TIMES .STRINGZ " times! "

CCOUNT .FILL x3300

.END

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How Do I Use A Database Research Tools You Can Use

Authors: Laura La Bella

1st Edition

1622753763, 978-1622753765

More Books

Students also viewed these Databases questions