2312 Slides 9

pdf

School

University of Texas, Arlington *

*We aren’t endorsed by this school

Course

2312

Subject

Computer Science

Date

Nov 24, 2024

Type

pdf

Pages

38

Uploaded by AmbassadorMule1343

Report
Slides 9: Register Convention, Strings in Memory, Live Coding with Strings (strlen, strfind, strfindn, strCat, strMid, uint32ToBinary, binaryToUint32) CSE2312: Computer Organization and Assembly Language Programming
R12 (IP): The Intra-Procedure-call scratch register Used to preserve PC for ‘BL’ in assembly program R13: Stack Pointer The address of the last element in stack (more on it later… …) R14: Link Register Preserve PC for ‘BL’ not in assembly program R15: Program Counter The address of the next instruction R0-R3: Scratch Register, use it however you want (Caller-saved register) R4-R11: Callee register (Callee-saved register) A caller is a function that calls another function A callee is a function that was called Preserve its content before use & restore its content after use (with stack) Register Convention
Stack Stack illustration A data structure located in Memory that follows a manner of Last-In-First-Out. Only read/write one data element from stack at each time. Write to a stack : Push: the data element is placed at the top of the stack (as the last element of the stack) Read from a stack: Pop: the top (last) data element is taken out of the stack We can only access the last element pushed into a stack Pop multiple times to reach elements in the middle or bottom of a stack
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Stack Stack illustration Stack address The computer maintains a pointer pointing to the stack top The pointer has the address of the last element pushed into stack R13 stores stack address, so it is also called SP (stack pointer).
Preserve Caller-saved Register with Stack Stack illustration What if you need more than R0-R3 in your assembly function? If you need extra registers, back them up before using it. Example: Push content of R4 into stack. Assembly: PUSH{R4} Remember to restore them before returning to caller (before the line: BX LR). Example: Pop the preserved content of R4 out of stack. Assembly: POP{R4}
char in memory Type char is 8-bit, one memory byte to store a char A variable of type char is stored as its corresponding ASCII code (a number) E.g., \ 0’ = 0, ‘0’ = 48, ‘1’ = 49, …, ‘9’ = 57, ‘A’ = 65, ‘B’ = 66, , ‘Z’ = 90, ‘a’ = 97, …, ‘z’ = 122 Data Type ‘char’
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Strings A string is an array of char (In C) every string ends with a special char ‘ \ 0’ (meaning Null, ASCII code 0) All chars within a string are stored together in memory, each occupies one memory byte The first char gets the lowest address The last char ‘ \ 0’ gets the highest address Address of string The address of a string is the address of its first char The name of this string is a pointer, pointing to its address Example: char str[] // a string named “str”, which is an array of char // str is a pointer, pointing to the first char in the string // address of the second char is str + 1, and so on
\ 0’ ‘d’ ‘l’ ‘r’ ‘o’ ‘w’ [space] ‘o’ ‘l’ ‘l’ ‘e’ ‘H’ Strings in memory char str[] = “Hello world”; // assume str = 0x00002000 (address) // * str = ‘H’ // char (8 bits) occupy one mem slot // ‘next character’: address + 1 // * (str+4) = str[4] = ‘o’ 0x0000200B 0x0000200A 0x00002009 0x00002008 0x00002007 0x00002006 0x00002005 0x00002004 0x00002003 0x00002002 0x00002001 0x00002000 (str)
extern uint32_t strLength(const char str[]); // return the count of all the characters in str[] except the null character \ 0’ // e.g., strLength(“Hello”) returns 5 Rough idea: Check chars in the string one by one and count meanwhile until we find \ 0’. String Coding: strLength
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
String Coding: strLength Prepare R0 for result: R0 to R1, 0 to R0. Load one char from str, adjust R1 pointing to next char Compare loaded char with ‘ \ 0’ Check Z flag BX LR Result (R0) increments by 1 Address of str[0] is now in R1 Address in R1 points to the next char Z set: the loaded char is ‘ \ 0’, noting the end of the string Z clear Branch back and check the next char Live coding: .global strLength .text strLength: MOV R1, R0 MOV R0, #0 sl_loop: LDRSB R2, [R1], #1 CMP R2, #0 ADDNE R0, R0, #1 BNE sl_loop BX LR
extern int32_t strFind(const char str[], char c); // returns the position of the first instance of char c in string // returns -1 if not found // e.g., strFind(“Hello”, ‘H’) returns 0 Rough idea: Load chars in str one by one and record index of current char If \ 0’, return -1 If not \ 0’, compare it with c Continue if not equal Return if equal String Coding: strFind
String Coding: strFind Live coding: .global strFind .text strFind: MOV R2, R0 MOV R0, #0 sf_loop: LDRSB R3, [R2], #1 CMP R3, #0 MOVEQ R0, #-1 BEQ sf_end CMP R3, R1 ADDNE R0, R0, #1 BNE sf_loop sf_end: BX LR Prepare R0 for result: R0 to R2, 0 to R0. Load one char from str, adjust R2 pointing to next char Compare the char with ‘ \ 0’ and check Z flag BX LR Result (R0) increments by 1 R0 for result, R1 has the char to find, Address of str[0] is in R2. Z set: end of string, target char not found Z clear: current char does not match target Branch back and check the next char -1 to R0 Compare the char with the target char in R1 Check Z flag Z set: target found
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
extern int32_t strFindN(const char str[], char c, uint32_t n); // return the position of the n-th instance of char c in string // returns -1 if not found // e.g., strFindN(“Hello world”, ‘l’, 3) returns 9 Rough idea: Load chars in str one by one and record index of current char If \ 0’, return -1 If not \ 0’, compare with c If not equal, continue If equal, n = n-1 if n == 0, return the index If n != 0, continue String Coding: strFindN
String Coding: strFindN Prepare R0 for result: R0 to R3, 0 to R0, PUSH{R4}. Load one char from str, adjust R3 pointing to next char. check Z flag POP{R4} BX LR R0 for result, R1 has the char to find, R2 counts remaining number of target char to find Address of str[0] is in R3. Preserve R4 with PUSH. Z set: end of string, target char not found Z clear: current char does not match target Branch back and check the next char. -1 to R0 Z set: the 𝑛 𝑡ℎ target char found, program end Z set: find the target char Z clear: Look for the next target char Restore content in R4 using POP Compare the char with ‘ \ 0’ Compare the char with target char c in R1 check Z flag decrement the counter (value n in R2) by 1 check Z flag Live coding: .global strFindN .text strFindN: //R0 has the address to the str, R1 has our target char, R2 is # of chars to be found MOV R3, R0 MOV R0, #0 PUSH {R4} //initial state START: LDRSB R4, [R3], #1 CMP R4, #’ \ 0’ MOVEQ R0, #-1 BEQ END CMP R4, R1 ADDNE R0, R0, #1 BNE START SUBS R2, R2, #1 BEQ END ADD R0, R0, #1 B START END: POP {R4} BX LR Result (R0) increments by 1
String Coding: strFindN Prepare R0 for result: R0 to R3, 0 to R0, PUSH{R4}. Load one char from str, adjust R3 pointing to next char. check Z flag POP{R4} BX LR R0 for result, R1 has the char to find, R2 counts remaining number of target char to find Address of str[0] is in R3. Preserve R4 with PUSH. Z set: end of string, target char not found Z clear: current char does not match target Branch back and check the next char. -1 to R0 Z set: the 𝑛 𝑡ℎ target char found, program end Z set: find the target char Z clear: Look for the next target char Restore content in R4 using POP Compare the char with ‘ \ 0’ Compare the char with target char c in R1 check Z flag decrement the counter (value n in R2) by 1 check Z flag Z set: find the target char n times Live coding: .global strFindN .text strFindN: PUSH {R4} MOV R3, R0 MOV R0, #0 sfn_loop: LDRSB R4, [R3], #1 CMP R4, #0 MOVEQ R0, #-1 BEQ sfn_end CMP R4, R1 ADDNE R0, R0, #1 BNE sfn_loop SUBS R2, R2, #1 ADDNE R0, R0, #1 BNE sfn_loop sfn_end: POP {R4} BX LR Result (R0) increments by 1
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
extern void strCat(char strTo[], const char strFrom[]); //append the content of strFrom to strTo //e.g., strTo[] = “Hello ”, strFrom[]=“World” strTo[]=“Hello World” Rough idea: Search for the tail of strTo[] (the address) Copy strFrom to the end of strTo char by char String Coding: strCat
String Coding: strCat Load one char from strTo, adjust R0 pointing to the next char Find the tail of strTo , i.e., ‘ \ 0’ Z set: the tail char of strTo is found (Note: address in R0 now is 1 memory byte advance to the string tail) Z clear: branch back and load the next char Live coding: .global strCat .text strCat:// R0 has the address of str_To, R1 has the address of str_From loop1: LDRSB R2, [R0], #1 CMP R2, #0 \\ #’ \ 0’ BNE loop1 SUB R0, R0, #1 loop2: LDRSB R2, [R1], #1 STRB R2, [R0], #1 CMP R2, #0 BNE loop2 BX LR Compare the char with ‘ \ 0’ check Z flag Adjust the pointer (R0) back to ‘ \ 0’ in strTo (R0 = R0 - 1) Load one char from strFrom, then adjust R1 pointing to the next char in strFrom Compare the loaded char with ‘ \ 0’ Store the char to address in R0 (pointing to the tail of strTo), then update R0 to next char in strTo check Z flag BX LR Z set: Reach the end of strFrom, exit the loop Z clear: strFrom still has char, continue
String Coding: strCat Load one char from strTo, adjust R0 pointing to the next char Find the tail of strTo , i.e., ‘ \ 0’ Z set: the tail char of strTo is found (Note: address in R0 now is 1 memory byte advance to the string tail) Z clear: branch back and load the next char Live coding: .global strCat .text strCat: LDRSB R2, [R0], #1 CMP R2, #0 BNE strCat SUB R0, R0, #1 sc_loop: LDRSB R2, [R1], #1 STRB R2, [R0], #1 CMP R2, #0 BNE sc_loop BX LR Compare the char with ‘ \ 0’ check Z flag Adjust the pointer (R0) back to ‘ \ 0’ in strTo (R0 = R0 - 1) Load one char from strFrom, then adjust R1 pointing to the next char in strFrom Compare the loaded char with ‘ \ 0’ Store the char to address in R0 (pointing to the tail of strTo), then update R0 to next char in strTo check Z flag BX LR Z set: Reach the end of strFrom, exit the loop Z clear: strFrom still has char, continue
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
extern void strMid(const char str[], char strTo[], uint32_t offset, uint32_t length); // return a substring starting at offset with max length // suppose offset is less than the length of str // e.g., strMid(“Hello world”, strTo, 6, 5) -> strTo[] = “world” // e.g., strMid(“Hello world”, strTo, 9, 5) -> strTo[] = “ld” Rough idea: Find the first char (address) in str to be copied Copy char by char until length chars copied or reach the end of str String Coding: strMid
String Coding: strMid Compare length (in R3) with 0 Locate the first char we copy to strTo (R0=R0+offset) check Z flag POP {R4} and BX LR Z set: done and exit Z clear: we need to copy chars Load one char from address R0 and update R0 pointing to the next char in the string (R0 = R0 + 1) Store the char into strTo (pointed by address in R1), then update R1 pointing to the next char (R1 = R1 + 1) Compare the char with ‘ \ 0’ Z set: reach the end of str, exit Reduce length by 1 (R3 = R3 - 1) Z clear: not the end of str, continue check Z flag Copy ‘ \ 0’ to the tail of strTo (address in R1) Z set: we copied the desired number of chars, add ‘ \ 0’ to the end of strTo and exit Z clear: the number of chars to be copies is not satisfied, branch back and continue Live coding: .global strMid .text strMid: // We need R4, PUSH {R4}; R0 has the address of strFrom, R1 has the address of str_To, R2 has offset, R3 has the length We need to use R4, so push it to the stack before the loop
String Coding: strMid Compare length (in R3) with 0 Locate the first char we copy to strTo (R0=R0+length) check Z flag BX LR Z set: done and exit Z clear: we need to copy chars Load one char from address R0 and update R0 pointing to the next char in the string (R0 = R0 + 1) Store the char into strTo (pointed by address in R1), then update R1 pointing to the next char (R1 = R1 + 1) Compare the char with ‘ \ 0’ Z set: reach the end of str, exit Reduce length by 1 (R3 = R3 - 1) Z clear: not the end of str, continue check Z flag Copy ‘ \ 0’ to the tail of strTo (address in R1) Z set: we copied the desired number of chars, add ‘ \ 0’ to the end of strTo and exit Z clear: the number of chars to be copies is not satisfied, branch back and continue Live coding: .global strMid .text strMid: PUSH {R4} CMP R3, #0 BEQ stm_end ADD R0, R0, R2 stm_loop: LDRSB R4, [R0], #1 STRB R4, [R1], #1 CMP R4, #0 BEQ stm_end SUBS R3, R3, #1 BNE stm_loop MOV R4, #0 \\ \ 0’ STRB R4, [R1] stm_end: POP {R4} BX LR
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
extern void uint32ToBinary (char str[], uint32_t x) //convert the x to a string representing the binary code of x stored in str[] // e.g., x = 1, str = “00000000000000000000000000000001” extern uint32_t binaryToUint32 (char str[]) // convert the null-terminated string (str) to uint32_t // treat the string as representing a binary number // assume the length of str is 32, and str only contains chars ‘0’ and ‘1’ as well as a \ 0’ at tail String Coding: uint32ToBinary & binaryTo Uint32
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
extern void uint32ToBinary (char str[], uint32_t x) // convert the binary code of x to a string stored in str[] // e.g., x = 1, str = “00000000000000000000000000000001” Rough idea From MSb to LSb , repeat the following process until all the 32 bits are converted to chars Examine one bit in x (shift or masking) Convert the bit to the corresponding char (‘0’ or ‘1’) Store this char to the tail of str String Coding: uint32ToBinary
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
String Coding: uint32ToBinary Initialize the mask as 0x80000000 Masking R1 (R1 & Mask) check Z flag Z set: store ‘0’ to address in R0, then update R0 to the new tail of str (R0 = R0 + 1) Update the mask for extracton of next bit (right shift 1 bit) Store ‘ \ 0’ to the tail of the string Z clear: new mask not all zeros yet, branch back and check the next bit Z clear: store ‘1’ to address in R0, then update R0 to the new tail of str (R0 = R0 + 1) check Z flag Z set: new mask has all zeros, all bits in x converted to char, end BX LR Live coding: .global uint32ToBinary .text uint32ToBinary:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
String Coding: uint32ToBinary Initialize the mask as 0x80000000 Masking R1 (R1 & Mask) check Z flag Z set: store ‘0’ to address in R0, then update R0 to the new tail of str (R0 = R0 + 1) Update the mask for extraction of next bit (right shift 1 bit) Store ‘ \ 0’ to the tail of the string Z clear: new mask not all zeros yet, branch back and check the next bit Z clear: store ‘1’ to address in R0, then update R0 to the new tail of str (R0 = R0 + 1) check Z flag Z set: new mask has all zeros, all bits in x converted to char, end BX LR Live coding: .global uint32ToBinary .text uint32ToBinary: MOV R2, #0x80000000 convert_loop: TST R1, R2 MOVEQ R3, #’0’ MOVNE R3, #’1’ STRB R3, [R0], #1 MOVS R2, R2, LSR #1 BNE convert_loop MOV R3, #0 STRB R3, [R0] BX LR
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
extern uint32_t binaryToUint32 (char str[]) // convert the null-terminated string (str) to uint32_t // treat the string as representing a binary number // assume the length of str is 32, and str only contains chars ‘0’ and ‘1’ as well as a \ 0’ at tail Rough idea Repeat the following process until reach the end of str Read one char from str Clear or set the corresponding bit in the result depending on the char (set if ‘1’, clear if ‘0’, with ORR) String Coding: b inaryTo Uint32
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
String Coding: b inaryTo Uint32 Prepare result in R0: move R0 to R1 and move 0 to R0. Then initialize the mask as 0x80000000 in R2 Load one char from the address R1 to R3 and then update R1 Compare the char in R3 with 0 check Z flag Z clear: not the end of str, continue Live coding: .global binaryToUint32 .text binaryToUint32: Assign 1 to the bit: R0 | Mask, then shift mask 1 bit to the right BX LR Z set: the loaded char is ‘1’ Z set: end of the str, exit Compare the char with ‘1’ Assign 1 to the bit: R0 | Mask, then shift mask 1 bit to the right Branch back to the next loop check Z flag
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
String Coding: b inaryTo Uint32 Prepare result in R0: move R0 to R1 and move 0 to R0. Then initialize the mask as 0x80000000 in R2 Load one char from the address R1 to R3 and then update R1 Compare the char in R3 with 0 check Z flag Z clear: not the end of str, continue Live coding: .global binaryToUint32 .text binaryToUint32: MOV R1, R0 MOV R0, #0 MOV R2, #0x80000000 convert_loop: LDRSB R3, [R1], #1 CMP R3, #0 BEQ convert_end CMP R3, #’1’ ORREQ R0, R0, R2 MOV R2, R2, LSR #1 BAL convert_loop convert_end: BX LR Assign 1 to the bit: R0 | Mask, then shift mask 1 bit to the right BX LR Z set: the loaded char is ‘1’ Z set: end of the str, exit Compare the char with ‘1’ Assign 1 to the bit: R0 | Mask, then shift mask 1 bit to the right Branch back to the next loop check Z flag
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Homework 4 Problem 2.a bool isStrEqual(const char str1[], const char str2[]) // returns true (1) if the strings match // false (0) otherwise Live coding: .global isStrEqual .text isStrEqual: //initial state: we need R0 for our output, so move address of str1 to R2, move 0 to R0 (not equal by default) //start the loop //Load one char from str1 then update address to next char //Load one char from str2 then update address to next char //CMP two char //if not equal, return (go to end section) //if equal, CMP the char with ‘ \ 0’ //if it is ‘ \ 0’, set result to 1, return // if not ‘ \ 0’, branch to start and check the next char //End section here
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Homework 4 Problem 2.a Live coding: .global isStrEqual .text isStrEqual: //initial state: we need R0 for our output, so move address of str1 to R2, move 0 to R0 (not equal by default) PUSH {R4} MOV R2, R0 MOV R0, #0 START: //start the loop LDRSB R3, [R2], #1 //Load one char from str1 then update address to next char LDRSB R4, [R1], #1 //Load one char from str2 then update address to next char CMP R3, R4//CMP two char BNE END //if not equal, return (go to end section) CMP R3, #0 //if equal, CMP the char with ‘ \ 0’ MOVEQ R0, #1 //if it is ‘ \ 0’, set result to 1, return BEQ END B START// if not ‘ \ 0’, branch to start and check the next char END: //End section here POP {R4} BX LR bool isStrEqual(const char str1[], const char str2[]) // returns true (1) if the strings match // false (0) otherwise
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Homework 4 Problem 2.c void leftString(char * strOut, const char * strIn, uint32_t length) //input: array (strIn) containing the input string, and the number of characters to extract (length) //output: array (strOut) containing up to, but not exceeding length number of strIn characters from the start of the array //strIn = ‘ abcdef ’, length = 5 → returns strOut = abcde //strIn = ‘ abcdef ’, length = 7 → returns strOut = abcdef global leftString .text leftString: //CMP length with 0 //if length = 0, branch to end (BX LR) //load one char from strIn, update address to the next char //store the char to strOut //compare the char with ‘ \ 0’ //if it is, branch to end //otherwise, subtract 1 from length // branch back to beginning // End section here
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Homework 4 Problem 2.c global leftString .text leftString: CMP R2, #0 //CMP length with 0 BEQ END //if length = 0, branch to end (BX LR) LDRSB R3, [R1], #1//load one char from strIn, update address to the next char STRB R3, [R0], #1 //store the char to strOut CMP R3, #0 //compare the char with ‘ \ 0’ BEQ END //if it is, branch to end SUB R2, R2, #1 //otherwise, subtract 1 from length B leftString // branch back to begining END: // End section here BX LR void leftString(char * strOut, const char * strIn, uint32_t length) //input: array (strIn) containing the input string, and the number of characters to extract (length) //output: array (strOut) containing up to, but not exceeding length number of strIn characters from the start of the array //strIn = ‘ abcdef ’, length = 5 → returns strOut = abcde //strIn = ‘ abcdef ’, length = 7 → returns strOut = abcdef
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Homework 4 Problem 2.d Int16_t decimalStringToInt16(const char* str) // convert the null-terminated string (str) to a signed 16-bit integer // treat the string as representing a decimal number // if a character other than 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, or is present or the value is out of range, return 0 // the -, if present after the first character should cause the function to return 0 \ 0’ ‘2’ ‘1’ ‘3’ ‘2’ (50) If it is a non-negative number: 1. Check first char (50), convert it to decimal 2= 50-48; 2. Check the second char (51), convert it to decimal 3 = 51-48; 2*10+3 =23 3. Check the next char (49), convert it to decimal 1= 49-48; 23*10+1=231 4. Check the next char (50), 2= 50-48, 231*10+2=2312 5. Check the next char, ‘ \ 0’. Reach the tail, so return 6. If the char is not a digit, return 0
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Homework 4 Problem 2.d Int16_t decimalStringToInt16(const char* str) What if it is a negative number? 0. If the first character is ‘ - ’, record it using a register. Updated the address pointing to the next char. The rest are the same as positive case 1. … \ 0’ ‘2’ ‘1’ ‘3’ ‘2’ -
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Homework 4 Problem 2.d global decimalStringToInt16 .text decimalStringToInt16: //initial state //prepare R0 for result, move address to R1 and initialize R0 as 0 //load the first char //compare with ‘ - //if equal, set the sign register to -1 //updates address to the next char //if not equal, set the sign register to 1 // Now, we have the address pointing to the string without sign. START: //loop starts here //load one char from memory with address R1, update R1 pointing to the next char //compare char with ‘ \ 0’ //if it is, branch to Check //compare char with 48 (‘0’) , if less, return 0 //compare char with 57, if greater, return 0 //convert char to corresponding decimal (char 48) //result * 10 //result = result + the decimal value //branch back to load and process the next char CHECK: //check whether current result is out of range //change the absolute value to the signed number //check whether current result is out of range, if less than -32,768 or greater than 32767, return 0 END: BX LR
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Homework 4 Problem 2.d global decimalStringToInt16 .text decimalStringToInt16: //initial state MOV R1, R0 //prepare R0 for result, move address to R1 and initialize R0 as 0 MOV R0, #0 LDRSB R2, [R1] //load the first char CMP R2, #’ - ’//compare with ‘ - MOVEQ R3, #-1 //if equal, set the sign register to -1; updates address to the next char ADDEQ R1, R1, #1 MOVNE R3, #1//if not equal, set the sign register to 1 // Now, we have the address pointing to the string without sign. START: //loop starts here LDRSB R2, [R1], #1 //load one char from memory with address R1, update R1 pointing to the next char CMP R2, #0 //compare char with ‘ \ 0’ BEQ CHECK //if it is, branch to Check CMP R2, #48 //compare char with 48 (‘0’) , if less, return 0 MOVLT R0, #0 BLT END CMP R2, #57 //compare char with 57, if greater, return 0 MOVGT R0, #0 BGT END SUB R2, R2, #48 //convert char to corresponding decimal (char 48) MUL R0, R0, #10 //result * 10 ADD R0, R0, R2 //result = result + the decimal value B START //branch back to load and process the next char CHECK: //check whether current result is out of range MUL R0, R0, R3 // change the absolute value to the signed number CMP R0, #-32768 //check whether current result is out of range, if less than -32,768 or greater than 32767, return 0 MOVLT R0, #0 CMP R0, #32767 MOVGT R0, #0 END: BX LR
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Homework 4 Problem 2.e uint8_t hexStringToUint8(const char * str) // convert the null-terminated string (str) to an unsigned 8-bit integer // treat the string as representing a hexadecimal number // if a character other than 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, or F is present or the value is too large, return 0. Step 1: Load ‘A’, a valid hex digit, convert ‘A’ to decimal by subtracting 55. (ASCII ‘A’ = 65). Integrate the char to result (initially 0): result = result * 16 + 10 = 10 Step 2: Load ‘1’, a valid hex digit, convert ‘1’ to decimal by subtracting 48. Integrate it to result: result = result *16 + 1 =161 Step 3: Load ‘B’, a valid hex digit, convert ‘B’ to decimal by subtracting 55. Integrate it to result: result = 161 * 16 + 11 = 2587 Step 4: Load ‘0’, a valid hex digit, convert ‘0’ to decimal by subtracting 48. Integrate it to result: result = 2587 * 16 + 0 = 41392 ((10*16+1)*16+12)*16+0 =10*16^3+1*16^2+12*16+0 \ 0’ ‘0’ ‘B’ ‘1’ ‘A’
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Homework 4 Problem 2.e
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help