2015-05-04 45 views
2

我正在用NASM編寫簡單的時鐘程序。我在OSX上通過iTerm使用Ubuntu 14.10 Vagrant框。終端是xterm,所以應該是VT-100兼容的。VT-100命令奇怪地工作

我需要刪除一行。舉例來說,我希望以下行爲:

Hello, this is clock program 
13:01:25 UTC+4 

下一刻:

Hello, this is clock program 
13:01:26 UTC+4 

我寫了下面的功能。對於打印:

func_print: 
    mov eax, sys_write 
    mov ebx, stdout 
    int 0x80 
    ret 

對於明確:

clr   db 0x1b, "[K" 
clr_len  equ $-clr 
... 
func_clear: 
    mov ecx, clr 
    mov edx, clr_len 
    call func_print 

保存和恢復位置我使用VT-100和它的命令:[7[8分別爲:

csave db  0x1b, "[7" 
csave_len equ $-csave 

crestore  db 0x1b, "[8" 
crestore_len equ $-crestore 

我的代碼:

global _start 
    _start: 
    mov ecx, welcome 
    mov edx, welcome_len 
    call func_print 

    call func_print 
    call func_save_cursor_pos 

    mov dword [tv_sec], 2 
    mov dword [tv_usec], 0 

    call func_sleep 
    call func_clear 

    call func_restore_cursor_pos 
    mov ecx, welcome 
    mov edx, welcome_len 
    call func_print 

    jmp func_exit 

然而,結果是:

[email protected]:~$ ./run.sh 
Hello, this is the clock program 
Hello, this is the clock program 








Hello, this is the clock program 
[email protected]:~$ 

我怎樣才能解決這個問題:

[email protected]:~$ ./run.sh 
Hello, this is the clock program 
Hello, this is the clock program 
Hello, this is the clock program 
[email protected]:~$ 

如果我加入[1A[1B似乎被刪除線,以比所需的更高或更低的太大變化clr ?什麼是正確的代碼?

+0

既然你只是更新一行,你是否考慮過使用'\ r'而不是? –

+0

這只是一個例子,我想更新任意數量的行,實際上 – ka2m

回答

1

我懷疑你的問題涉及暗示在welcome db "Hello, this is the clock program", 10的換行符。我無法確定,因爲你沒有發佈你的部分代碼。

我認爲這會導致問題,因爲換行符導致終端滾動 - 當我從我的版本中刪除換行符時,它正常工作。如果你只需要更新一行,就可以不用換行。

我懷疑保存和恢復操作是在屏幕上的字面物理位置上工作的,而不是通過換行符滾動的邏輯位置。

但一般情況下,我建議你使用光標操縱轉義代碼代替:

  • 當你準備好重繪你的輸出,寫db 0x1b, "[nA"上移n行。 (您需要將該號碼放在那裏。)
  • 緊隨其後(或之後的任何換行符),請編寫db 0x1b, "[K"以清除該行。 (你已經知道這一點,但我包括它的完整性。)

我寫了一個示例程序來實現這個,部分基於你的。它顯示:

Hello, this is the clock program. 
Line two. 

然後,一小會兒後

=== TEST === 
More. 

然後

=== TEST 2 === 
Again. 

這種技術應該是推廣到線中的任意合理數量。

BITS 32 
section .text 

welcome  db "Hello, this is the clock program", 10, "Line two.", 10 
welcome_len equ $-welcome 

test_str  db 0x1b, "[2A", 0x1b, "[K=== TEST ===", 10, 0x1b, "[KMore.", 10 
test_len  equ $-test_str 

test2_str  db 0x1b, "[2A", 0x1b, "[K=== TEST 2 ===", 10, 0x1b, "[KAgain.", 10 
test2_len  equ $-test2_str 

func_print: 
    mov eax, 4 
    mov ebx, 1 
    int 0x80 
    ret 

pause: ; Note: DON'T EVER USE THIS IN A REAL PROGRAM. This is not how you sleep properly. 
    mov eax, 0 
loop: 
    inc eax 
    cmp eax, 1000000000 
    jl loop 
    ret 

global _start 
_start: 
    mov ecx, welcome 
    mov edx, welcome_len 
    call func_print 

    call pause 

    mov ecx, test_str 
    mov edx, test_len 
    call func_print 

    call pause 

    mov ecx, test2_str 
    mov edx, test2_len 
    call func_print 

    mov eax, 1 
    mov ebx, 0 
    int 0x80 
+0

非常感謝!兩個版本都有效 – ka2m