Question

In: Computer Science

Write hack assembly language code for function (declaration) call (a function) return (from a function)

Write hack assembly language code for

function (declaration)

call (a function)

return (from a function)

Solutions

Expert Solution

// Main code that calls the routine FRED

        ...    // Get desired argument value in D

        @RET_A

        D=A    // Put Return Address in D

        @R0

        M=D    // Store Return Address stored in R0

        @FRED

        0; JMP // Call FRED

(RET_A)

        ...    // Return value is in D

        @END

(END)

        0; JMP // Infinite loop to stall simulator

Now we just have to make some modifications to our routine to be consistent with using the scratchpad.

// ==========================================================

// ROTL1

// ----------------------------------------------------------

// Description:

//    Rotates the value in the D register left 1 bit

// ----------------------------------------------------------

// Arguments:

//    D - value to be rotated

//    R0 - Return address

// Return values:

//    D - rotated value

// Local variables:

//    R1 - data being worked on

//    R2 - lsb value to be added to shift to finish rotate

// ----------------------------------------------------------

(ROTL1)

        @R2

        M=0    // Initialize lsb mask to 0

        @R1

        D=M    // Load data value into memory

        @ROTL1_SKIP

        D; JGE // Skip the next step if msb is zero

        @R2

        M=1    // Set lsb to 1 to match msb

(ROTL1_SKIP)

        @R1

        MD=D+M // Shift data left by 1 (D still has data in it)

        @R2

        D=D+M // Add in the lsb

        @R0    // R0 is NOT the return address

        A=M    // It is were the return address is stored

        0; JMP // Return to calling code

// ==========================================================

Now let's flesh out the "main" code to rotate a value more than one position. We'll assume that the top level code is using the scratchpad to store its data, which will mean that we will have to save those values to higher memory before calling our routine and then restore them afterwards.

// ==========================================================

// MAIN

// ----------------------------------------------------------

// Description:

//    Main code that uses the ROTL1 to rotate a value left

//    by an arbitrary amount

// ----------------------------------------------------------

// Arguments:

//    R0 - value to be rotated

//    R1 - amount to rotate by

// Return values:

//    D - rotated value

// ----------------------------------------------------------

(MAIN)

        // Set up the memory with some test data

        @100   // Value to be rotated

        D=A

        @R0    // R0 holds the value to be rotated

        M=D

        @7     // Number of positions to rotate the value

        D=A   

        @R1    // R1 holds the rotate amount

        M=D

        // Loop to rotate value in R1 the amount in R0

(MAIN_TEST)

        @R1

        D=M

        @MAIN_LOOP_END

        D;JLE // Exit loop when shift amount is <=0

        // --------------------------------------------------

        // Use ROTL1 to rotate value in D left 1 bit

        // --------------------------------------------------

        // Transfer scratchpad registers to safe area

        @R0    // Transfer R0 to R8

        D=M

        @R8

        M=D

        @R1    // Transfer R1 to R9

        D=M

        @R9

        M=D

        // CALL ROTL1

        @RET_A

        D=A    // Put Return Address in D

        @R0

        M=D    // Store Return Address stored in R0

        @R8

        D=M    // Get desired value in D (from safe store)

        @ROTL1

        0; JMP // Call ROTL1

(RET_A)

        // Plase return value into scratchpad

        @R0    // Return value in D goes into R0

        M=D   

        // Restore rest of scratchpad

        @R9 // Transfer R9 to R1

        D=M

        @R1

        M=D

        // --------------------------------------------------

        @R1

        MD=M-1 // Decrement rotate amount

        @MAIN_TEST

        0; JMP

(MAIN_LOOP_END)

        @R1

        D=M    // Final rotated amount is in D

        @END

(END)

        0; JMP // Infinite loop to stall simulator


Related Solutions

Write hack assembly language code for eq lt gt
Write hack assembly language code for eq lt gt
How to write fill.asm code (hack assembly)
How to write fill.asm code (hack assembly)
hack assembly language code for eq(equal), gt(greater than) and lt(less than).
hack assembly language code for eq(equal), gt(greater than) and lt(less than).
write a program for the microcontroller-msp430fr6989 using code composer studio not assembly language. write a code...
write a program for the microcontroller-msp430fr6989 using code composer studio not assembly language. write a code that transmits a single character and lights the red LED upon receiving that character. The board will "talk" to itself. The green LED should turn on whenever a message is sent and the LCD will display the message being received.
arms64 assembly language Write a function to read a Sudoku board from an input string. The...
arms64 assembly language Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5...
write a assembly language program to convert GRAY to BCD code in 8051
write a assembly language program to convert GRAY to BCD code in 8051
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to...
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to the follow java program: int day = (int)(Math.random() * 7); switch (day) { case 1: System.out.println(“Monday”); break case 2: System.out.println(“Tuesday”); break case 3: System.out.println(“Wednesday”); break case 4: System.out.println(“Thursday”); break case 5: System.out.println(“Friday”); break case 6: System.out.println(“Saturday”); break case 0: System.out.println(“Sunday”); break }
Write a MIPS assembly language to transpose a square integer matrix in code
Write a MIPS assembly language to transpose a square integer matrix in code
Language Assembly ( required) Write and test a function, drawshape. The function has 2 parameters, the...
Language Assembly ( required) Write and test a function, drawshape. The function has 2 parameters, the shape's character and the length of the shape's longest line. draw shape(1, x) looks displays x x draw shape(2, y) displays yy y yy draw shape(3, z) displays zzz zz z zz zzz and so on.
Language Assembly ( required) Write and test a function, drawshape. The function has 2 parameters, the...
Language Assembly ( required) Write and test a function, drawshape. The function has 2 parameters, the shape's character and the length of the shape's longest line. draw shape(1, x) looks displays x x draw shape(2, y) displays yy y yy draw shape(3, z) displays zzz zz z zz zzz and so on.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT