DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Assembly (http://www.daniweb.com/forums/forum125.html)
-   -   Need help in LC# code (http://www.daniweb.com/forums/thread60655.html)

qaizaar Nov 5th, 2006 6:50 pm
Need help in LC# code
 
how do i read an input in LC-3 where it checks for yes or no as an answer to a prompt and returns 1 if yes and no if 0. thanks

Ancient Dragon Nov 6th, 2006 1:14 pm
Re: Need help in LC# code
 
you should probably get the recommended book Introduction to Computing Systems: From Bits and Gates to C and Beyond and read it. I don't know the first thing about it, so can't help you.

c0dex Dec 4th, 2008 11:26 pm
Re: Need help in LC# code
 
You're just in luck. I just finished writing a similar subroutine for my assembly class (uses the same textbook as mentioned above) to ask if someone wants to play again (we've made the game pong breakout). The following code is documented well enough for you to understand it clearly.

; 
; Prompts the user for something.
;
; Preconditions:
;
;    TRAP x42 must be implemented in the OS in a way that it does not wait
;    for a key to be pressed. It should only check if a key's pressed once.
;
;        Must use the following declarations.
;
;        PROMPT .STRINGZ "Would you like to do something (y/n)? "
;        HEX_Y1 .FILL x79                        ; hex for 'y'
;        HEX_Y2 .FILL x59                        ; hex for 'Y'
;        HEX_N1 .FILL x6E                        ; hex for 'n'
;        HEX_N2 .FILL x4E                        ; hex for 'N'
;
; Postconditions:
;
;        If yes, return 1 in R0.
;        If no, return 0 in R0.
;
PROMPT_SR
        LEA R0, PROMPT                        ; Load PROMPT to R0 be a string.
        TRAP x22                                ; Print the string in R0.

POLL        TRAP x42                                ; Get the key press. (This code assumes that
                                                ; TRAP x42 does not wait for a key to be pressed.

        LD R4, HEX_Y1                        ; Load the hex value for 'y'.
        NOT R4, R4;                                ; Get the 2's compliment
        ADD R4, R4, #1                        ;
        ADD R4, R5, R4                        ; R4 = R5 - R4
       
        BRz YES                                ; If R5 is zero, then 'y' was pressed.

        LD R4, HEX_Y2                        ; Load the hex value for 'Y'.
        NOT R4, R4;                                ; Get the 2's compliment
        ADD R4, R4, #1                        ;
        ADD R4, R5, R4                        ; R4 = R5 - R4
       
        BRz YES                                ; If R5 is zero, then 'Y' was pressed.

        LD R4, HEX_N1                        ; Load the hex value for 'n'.
        NOT R4, R4;                                ; Get the 2's compliment
        ADD R4, R4, #1                        ;
        ADD R4, R5, R4                        ; R4 = R5 - R4
       
        BRz NO                                ; If R5 is zero, then 'n' was pressed.

        LD R4, HEX_N2                        ; Load the hex value for 'N'.
        NOT R4, R4;                                ; Get the 2's compliment
        ADD R4, R4, #1                        ;
        ADD R4, R5, R4                        ; R4 = R5 - R4
       
        BRz NO                                ; If R5 is zero, then 'N' was pressed.

        BRnzp POLL                                ; y/Y or n/N has not bee pressed, so poll again.

YES:
        AND R0, R0, #0                        ; Clear R0.
        ADD R0, R0, #1                        ; Add one to R0.
        RET

NO:       
        AND R0, R0, #0                        ; Clear R0.
        RET


All times are GMT -4. The time now is 4:52 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC