Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Declare a string in the data section such as: .data string: .asciiz \"in a hole

ID: 3586624 • Letter: D

Question

Declare a string in the data section such as:

.data

string: .asciiz "in a hole in the   ground there lived a hobbit"

Notice there are extra spaces in this string. Write an MIPS program that capitalizes the first letter of each word, so that after running your program the data will look like this:

.data

string: .asciiz "In A Hole In The   Ground There Lived A Hobbit"

Rewrite your program so that it assumes that the data comprises only upper and lower case characters and spaces, and alters a character only if it is lower case, and follows a space or is the first character on the line.

Print the strings before and after translation by using the syscall print string service.

Explanation / Answer

Hi,

Please try below code-

[BITS 16]

[ORG 0x100] ; DOS .com program, no header, CS=DS

EntryPoint:

mov ax, 0x03 ; set text mode 3 - we should already be in this mode, done to clear the screen

int 0x10

push word myMessage

call makePrettyCase

push word myMessage

call printString

int 0x20 ; exit back to dos

;void printString(*str)

printString:

push bp

mov bp, sp

add bp, 4

mov si, [bp+0] ; ds:si ---> string

.getChar:

lodsb

test al, 0xff ; sets the flags as if we did 'and al, 0xff' - if the zero flag is set, there are no bits set in al, i.e it's 0

jz .printStringDone

mov ah, 0xe

int 0x10

jmp .getChar

.printStringDone:

pop bp

ret 2

; input:

; AL = char

; outpt:

; if al if an alpha char, ensures it is in range A-Z

toupper:

cmp al, 'a'

jb .toupperDone

cmp al, 'z'

ja .toupperDone

add al, 'A' - 'a'   

.toupperDone:

ret

; input:

; AL = char

; outpt:

; if al if an alpha char, ensures it is in range a-z

tolower:

cmp al, 'A'

jb .tolowerDone

cmp al, 'Z'

ja .tolowerDone

sub al, 'A' - 'a'   

.tolowerDone:

ret

; void makePrettyCase(char *string)

makePrettyCase:

push bp

mov bp, sp

add bp, 4 ; add 2 for the bp we just saved, and 2 for the return address. bp now points at out input var, a pointer to the string

pusha

mov si, [bp+0] ; point source index to string

mov di, si ; point dest index to string

xor dl, dl ; DL = lastChar, holds 0 for the first char in the string

mov dh, ' ' ; DH = spaceChar (' ') - holds the ascii code for a space

.getNextChar:

lodsb ; ds:[si] --> al, si = si+1

test al, 0xff

jz .makePrettyCaseDone

.checkIfFirstInString: ; if so, capitalize

; cmp byte [lastChar], 0

cmp dl, 0

jne .checkIfLastWasSpace

jmp .makeUpper

.checkIfLastWasSpace: ; if so, capitalize

;cmp byte [lastChar], spaceChar

cmp dl, dh

jne .makeLower

.makeUpper:

call toupper

stosb ; al --> es:[di], di = di+1

mov dl, al ; save this char as our lastChar

;mov byte [lastChar], al

jmp .getNextChar

.makeLower:

call tolower

stosb ; al --> es:[di], di = di+1

mov dl, al ; save this char as our lastChar

;mov byte [lastChar], al

jmp .getNextChar

.makePrettyCaseDone:

popa

pop bp

ret 2 ; return and remove the 2 bytes of the input var from the stack

;==========================================

[section .data]

myMessage db "in a hole in the ground there lived a hobbit",0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote