1 / 12

ECE 353 Introduction to Microprocessor Systems

ECE 353 Introduction to Microprocessor Systems. Discussion 2. Topics. Procedures and Subroutines Stacks Q & A. Problem - Procedures.

errin
Download Presentation

ECE 353 Introduction to Microprocessor Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 353Introduction to Microprocessor Systems Discussion 2

  2. Topics • Procedures and Subroutines • Stacks • Q & A

  3. Problem - Procedures • Write a procedure that implements the functionality of the c function atoi(). This function converts the ASCIIZ decimal string to its equivalent 32-bit value. • The only allowable characters in the string are an optional leading ‘+’ or ‘-’, at least one of the digits ‘0’-‘9’, and the null terminator. • You may assume that there are no errors in the string, and that the number will not overflow. • Your subroutine will be passed the starting address of the string in R1, and is to return the result in R0. • Also write a main program to setup and call the procedure.

  4. Answer • Main: • Define string1 (null terminate). • Setup R1 with string addresses. • Call atoi. • Spin • atoi: • Save registers if needed. • Initialize the result and check for a ‘+’ or a ‘-’ • atoi_loop – to take each character of the string and convert it to binary. • Multiply the previously calculated result by 10 and then add the newly converted binary number to the result • Must also check for end of string • Exit condition – end of string • Do calculations based on the sign preceding the number • Retrieve the saved registers

  5. Problem - Procedures • Write a subroutine that functions like the C language strspn function. Your subroutine will return the length of the initial portion of one string (str1) that consists only of characters that are part of a second string (str2). • You are to write a subroutine named strspn that implements strspn, assuming that the address of str1 is passed in R1 and the address of str2 is passed in R2. Both strings will be ASCIIZ (zero-terminated). Return the length in R0 • One or both strings may be zero length, in both cases you should return 0. If the first character in str1 is not present in str2, return 0. You may not disturb any of the caller’s registers except R0.

  6. Answer • Main: • Define str1 and str2 (null terminate). • Setup R1 and R2 with string addresses. • Call strspn. • Spin • strspn: • Save registers if needed. • Initialize the result and check for end of string in str1 and str2 • Strspn – loop: • check if the first character of str1 is part of str2 • If present, increment count and check if the next character of str1 is part of str2 • Proceed the above step until end of string of str1 or until a character of str1 not found in str2 • Return the count • If the first character of str1 is not part of str2, return 0 • Retrieve the saved registers

  7. Answer ; Filename: strspn.s ; Author: ECE 353 staff ; Description: subroutine to implement the strspn function ; Assumes: R1 contains the address of ASCIIZ str1 ; R2 contains the address of ASCIIZ str2 ; Returns: R0 contains the result of the strspn function ; Modifies: Nothing AREA FLASH, CODE, READONLY ARM EXPORT strspn strspn ; subroutine PUSH {R1-R5, LR} ; context save MOV R0, #0 ; initializing the result LDRB R3, [R1], #1 ; load the first character of str1 CMP R3, #0 ; check if str1 has zero length BEQ exit ; exit if the str1 has zero length LDRB R4, [R2] ; load the first character of str2 CMP R4, #0 ; check if str1 has zero length BEQ exit ; exit if the str2 has zero length

  8. Answer loop2 ; MOV R5, #0 ; next address pointer loop1 ; CMP R3, R4 ; compare the chars of str1 and str2 ADDEQ R0, R0, #1 ; if the characters are same, increment result BEQ next ; prepare to check the next char of str1 ADD R5, R5, #1 ; if a match was not found LDRB R4, [R2, R5] ; load the next char of str2 for checking CMP R4, #0 ; check for end of string BNE loop1 ; if no end of string, repeat the above steps B exit ; exit if end of string is encountered next LDRB R3, [R1], #1 ; load the next char of str1 in R3 CMP R3, #0 ; check for end of string LDRBNE R4, [R2] ; load the first char of str2 in R4 BNE loop2 ; re-start the checking process exit ; exit POP {R1-R5, PC} ; context restore END

  9. Stacks • Full vs. Empty • Full – SP points to the last location that was populated with a value (a full location) • Empty - SP points to the next available location where a value can be placed (an empty location) • Ascending vs. Descending • Ascending – starts at the bottom of the stack space and grows upward (increasing addresses) • Descending - starts at the top of the stack space and grows downward (decreasing addresses)

  10. Quiz : Stacks • What type of stack would each of the following instructions be used with? (e.g., FD, FA, ED, EA) • STR R0, [R13], #-4 • LDR R0, [R13], #4 • STR R0, [R13], #4 • LDR R0, [R13], #-4 • STR R0, [R13, #-4]! • LDR R0, [R13, #4]! • STR R0, [R13, #4]! • LDR R0, [R13, #-4]!

  11. Quiz : Stacks - Answers • STR R0, [R13], #-4 ;ED • LDR R0, [R13], #4 ;FD • STR R0, [R13], #4 ;EA • LDR R0, [R13], #-4 ;FA • STR R0, [R13, #-4]! ;FD • LDR R0, [R13, #4]! ;ED • STR R0, [R13, #4]! ;FA • LDR R0, [R13, #-4]! ;EA

  12. Questions?

More Related