簡體   English   中英

摩托羅拉68000組裝式存儲

[英]Motorola 68000 Storage in Assembly

編寫該程序是為了使用四個標記來分隔用戶輸入的空間,如果標記都是數字,則將它們加在一起,然后將結果打印到終端。 現在,它適用於“ 1 1 1 1”和“ 123 123 123 123”之類的數字,但是當我嘗試添加4個七位數長的數字(7個數字,因為那是極端情況)時,它只會添加最高可達23068。這聽起來像是我的一個標簽或其他東西的尺碼問題,但我不確定。

這是代碼:

*
        ORG     $0
        DC.L    $3000           * Stack pointer value after a reset
        DC.L    start           * Program counter value after a reset
        ORG     $3000           * Start at location 3000 Hex
*
*----------------------------------------------------------------------
*
#minclude /home/cs/faculty/cs237/bsvc/macros/iomacs.s
#minclude /home/cs/faculty/cs237/bsvc/macros/evtmacs.s
*
*----------------------------------------------------------------------
*
* Register use
*
*----------------------------------------------------------------------
*
start:  initIO                  * Initialize (required for I/O)
    setEVT          * Error handling routines
*   initF           * For floating point macros only    

                * Your code goes HERE
*Output info:               
    lineout header      *Display header info
    lineout prompt      *Display prompt
    linein  buffer      *Read input to buffer

    lea buffer,A1       *
    adda.l  D0,A1       *Add null terminator
    clr.b   (A1)        *

    lea buffer,A1       *Reload the beginning of buffer address

    move.l  #1,D1       *D1 is input counter and starts at 1

    clr.l   D2          *
    clr.l   D3          *Prepping registers for calculations
    move.l  #0,result   *

    move.l  A1,A2       *Duplicating address to use for strlen

top:    
    tst.b   (A1)        *Check for end of string
    BEQ rest            *If end, go to rest

    cmpi.b  #47,(A1)    *Check current byte against low end of ascii numbers
    BGT toprange        *This means byte *might* be an ascii number

    cmpi.b  #32,(A1)    *Byte is below range for numbers. Is it a space?
    BNE notno           *If this triggers, it's not a space and not a number. Exit.

    cmpi.b  #32,1(A1)   *Is the character after this a space? If yes, loop to top.
    BNE addit           *If not, it's either another valid byte or null terminator. 

    adda.l  #1,A1       *Increment token counter and loop to top.
    BRA     top 

toprange:   
    cmpi.b  #57,(A1)    *Is byte value higher than ascii numbers range?
    BGT notno           *If yes, it's not an ascii number. Exit. 

    cmpi.b  #32,1(A1)   *Is the byte after this a space?
    BEQ endoftoken      *If yes, that means this is the end of the token.

    tst.b   1(A1)       *If not, is this byte a null terminator?
    BEQ endoftoken      *If yes, this is the last token. Add it.

    adda.l  #1,A1       *Else increment the address pointer and loop.
    BRA top

endoftoken: 
    adda.l  #1,A1       *Increment pointer
    move.l  A1,D2       *
    sub.l   A2,D2       *Find length of token

    cvta2   (A2),D2     *Convert current token segment to number
    add.l   D0,result   *Add converted number to result address.
    BRA     top         *Loop to top.

addit:          
    tst.b   1(A1)       *Test for null
    BEQ endoftoken      *If null, go endof token to add it to running total

    addi.l  #1,D1       *If next byte isn't null, there might be more tokens. Incr & cont.
    adda.l  #1,A1   

    move.l  A1,A2       *Shift token starting point pointer forward
    BRA top 

rest:
    cmpi.l  #4,D1       *Check to make sure we have 4 tokens
    BNE incnums         *If not, exit on error

    move.l  result,D0   *Convert numbers back to text representations
    ext.l   D0
    cvt2a   result,#8
    stripp  result,#8
    lea result,A0
    adda.l  D0,A0
    clr.b   (A0)

    lea sum,A1          *Point to first bit of text for strcat
    lea output,A2       *Point to destination during copying
strcat1:
    tst.b   (A1)        *Null?
    BEQ strcat2         *Go to next segment of code
    move.b  (A1)+,(A2)+ *If not null, copy from A1 to A2. Post increment
    BRA strcat1

strcat2:
    move.b  #32,(A2)+   *Append space. Post increment
    lea result,A1       *Point to calculated result

strcat3:    
    tst.b   (A1)        *Is this byte null?
    BEQ printr          *If yes, go to print response.  
    move.b  (A1)+,(A2)+ *If not, copy byte to output string. 
    BRA strcat3

printr: 
    move.b  #46,(A2)+   *Append period to output string. 
    clr.b   (A2)        *Null terminate the string. 
    lineout output      *Print built string to terminal.
    BRA end 

incnums:
    lineout incno       *If here, there were not the correct number of tokens.
    BRA end


notno:
    cmpi.b  #1,D1       *This checks the token counter to determine which token was not a #
    BNE ch2
    lineout bn1
    BRA end

ch2:    
    cmpi.b  #2,D1
    BNE ch3
    lineout bn2
    BRA     end

ch3:    
    cmpi.b  #3,D1
    BNE ch4
    lineout bn3
    BRA end

ch4:    
    lineout bn4


end:



*Output result




        break                   * Terminate execution
*
*----------------------------------------------------------------------
*       Storage declarations

prompt: dc.b    'Enter the four space separated numbers:',0
sum:    dc.b    'The sum is',0
incno:  dc.b    'There are not four inputs.',0
buffer: ds.b    80
result: ds.l    3
output: ds.l    3
bn1:    dc.b    'The #1 input is not a number',0
bn2:    dc.b    'The #2 input is not a number',0
bn3:    dc.b    'The #3 input is not a number',0
bn4:    dc.b    'The #4 input is not a number',0
        end

編輯1

當我將ascii表示形式轉換為實際數字時,這似乎與它有關。 我將add.l添加到結果標簽。 標簽足夠大以存儲字符,但是我沒有向其移動足夠大的塊。

當我輸入“ 9999999 9999999 9999999 9999999”並設置一個斷點以觀看它時,內存將正確顯示26259FC的十六進制值,因此當我使用提供的宏將其轉換回時,這是一個問題。

我不希望有人為此提供解決方案,但也許有人可以。

Edit2:此代碼已在Sep Rowland的指導下進行了修訂(非常感謝)。 我認為他已經涵蓋了所有內容,並且修訂后的代碼已作為答案提交。

最初的問題: ext.l指令。 Sep Rowland在提供的其他答案之一中對此進行了解釋。

其他修訂:在9月的指導下,我在幾個方面優化了代碼。

+1用於內存查看器和斷點。

* Problem statement: Read input and determine if 4 numbers are provided. Add numbers.
* Input: ### ### ### ###
* Output: "The sum is ###"
* Error conditions tested: Correct number of data provided. Number vs Char
*  Also handles leading white spaces/multiple spaces between tokens
* Included files: None
* Method and/or pseudocode: 
* References: 
*----------------------------------------------------------------------
*
        ORG     $0
        DC.L    $3000           * Stack pointer value after a reset
        DC.L    start           * Program counter value after a reset
        ORG     $3000           * Start at location 3000 Hex
*
*----------------------------------------------------------------------
*
#minclude /home/cs/faculty/cs237/bsvc/macros/iomacs.s
#minclude /home/cs/faculty/cs237/bsvc/macros/evtmacs.s
*
*----------------------------------------------------------------------
*
* Register use
*
*----------------------------------------------------------------------
*
start:  initIO                  * Initialize (required for I/O)
    setEVT          * Error handling routines
*   initF           * For floating point macros only    


    lineout header      *Display header info
    lineout prompt      *Display prompt
    linein  buffer      *Read input to buffer
    lea buffer,A1       *
    adda.l  D0,A1       *Add null terminator
    clr.b   (A1)        *   
    lea buffer,A1       *Reload the beginning of buffer address
    clr.l   D1          *D1 is input counter and starts at 0
    clr.l   D2          *D2 used as workspace
    move.l  #0,result   *Clearing garbage out of memory address
    move.l  A1,A2       *A2 used for strlen

top:    
    tst.b   (A1)        *Check for end of string
    BEQ rest            *If end, go to rest

    cmpi.b  #47,(A1)    *Check current byte against low end of ascii numbers
    BGT checktoprange   *This means byte *might* be an ascii number

    cmpi.b  #32,(A1)
    BNE notnumber

whitespace:             *This will eat whitespace anywhere in buffer
    addq.l  #1,A1       *If we are here, we know current location is space
    cmpi.b  #32,(A1)    *So increment pointer and check for additional spaces
    BEQ whitespace
    move.l  A1,A2       *If we are here, we encountered a token
    BRA top             *Shift our pointer for token start location


checktoprange:  
    cmpi.b  #57,(A1)    *Is byte value higher than ascii numbers range?
    BGT notnumber       *If yes, it's not an ascii number. Exit. 

    cmpi.b  #32,1(A1)   *Is the byte after this a space?
    BEQ endoftoken      *If yes, that means this is the end of the token.

    tst.b   1(A1)       *If not, is this byte a null terminator?
    BEQ endoftoken      *If yes, this is the last token. Add it.

    addq.l  #1,A1       *Else increment the address pointer and loop.
    BRA top

endoftoken: 
    addq.l  #1,A1       *Increment pointer
    move.l  A1,D2       *
    sub.l   A2,D2       *Find length of token

    cvta2   (A2),D2     *Convert current token segment to number

    add.l   D0,result   *Add converted number to result address.
    addq.l  #1,D1       *Increment token counter
    BRA     top         

rest:
    cmpi.l  #4,D1       *Check to make sure we have 4 tokens
    BNE incnums         *If not, exit on error

    move.l  result,D0   *Convert numbers back to text representations
    cvt2a   result,#8
    stripp  result,#8
    lea result,A0
    adda.l  D0,A0
    move.b  #46,(A0)+
    clr.b   (A0)
    lineout sum
    BRA end


incnums:
    lineout incno       *If here, there were not the correct number of tokens.
    BRA end


notnumber:
    lea bn,A1
    addi.b  #49,D1       *From number to character
    move.b  D1,5(A1)     *Replaces the dot in the error message
    lineout bn

end:    

        break                   * Terminate execution
*
*----------------------------------------------------------------------
*       Storage declarations

header: dc.b    'This is a header',0
prompt: dc.b    'Enter the four space separated numbers:',0
incno:  dc.b    'There are not four inputs.',0
buffer: ds.b    80
bn:     dc.b    'The #. input is not a number',0
sum:    dc.b    'The sum is '
result: ds.l    1


        end

錯誤(您已經發現)是ext.l D0 鑒於此指令將D0的低位字符號擴展,因此結果不令人驚訝。


我很少有機會研究一些好的68K代碼,因此在這里我附有一些注釋,可以改善您的程序。

 addi.l #1,D1 adda.l #1,A1 

您可以編寫從#1到#8相加的這些小添加,如果使用addq指令,則最佳選擇:

addq.l  #1,D1
addq.l  #1,A1

要在某個輸入不是數字時顯示錯誤消息,如果將D1 (1-4)中的數字轉換為字符(“ 1”-“ 4”)並將其寫入單個錯誤中,則可以更簡單地編寫該錯誤消息信息:

lea     bn,A1
addi.b  #48,D1   *From number to character
move.b  D1,5(A1) *Replaces the dot in the error message
lineout bn

...

bn:     dc.b    'The #. input is not a number',0

 output: ds.l 3 

輸出緩沖區不足以執行您的操作!
您只有12個字節,但是您首先要復制10個字符的長消息,添加一個空格,添加幾個字符的長結果,添加一個句點並添加一個零。 顯然,緩沖區溢出。
現在,您可以延長此緩沖區的長度,或者明智地停止復制周圍的所有內容,而只是將結果緩沖區放在sum消息旁邊(帶有空格並沒有終止的零)。 然后一次性顯示組合的總和結果文本。 一個簡單得多的解決方案。

move.l  result,D0   *Convert numbers back to text representations
cvt2a   result,#8
stripp  result,#8
lea     result,A2
adda.l  D0,A2
move.b  #46,(A2)+   *Append period to output string. 
clr.b   (A2)        *Null terminate the string. 
lineout sum         *Print built string to terminal.
BRA end 

...
sum:    dc.b    'The sum is '
result: ds.l    3

 cmpi.b #32,1(A1) *Is the character after this a space? If yes, loop to top. BNE addit *If not, it's either another valid byte or null terminator. adda.l #1,A1 *Increment token counter and loop to top. BRA top toprange: 

您可以在此處在代碼中創建快捷方式,從而加快程序速度。
無須BRA一路到頂部 ,在那里你會被不必要地做3次試驗。

SkipWhitespace:
    cmpi.b  #32,1(A1)
    BNE     addit
    addq.l  #1,A1
    BRA     SkipWhitespace
toprange:   

  move.l A1,A2 *Duplicating address to use for strlen top: ... move.l A1,A2 *Shift token starting point pointer forward BRA top rest: 

始終不要寫多余的指令。

topEx:
    move.l  A1,A2       *Duplicating address to use for strlen
top:    

    ...

    BRA     topEx
rest:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM