两次调用sys_read [Nasm,Linux]

时间:2012-10-27 17:43:05

标签: linux io nasm

我尝试从输入中读取字符两次,第一次“调用getUserAction”工作正常,但主要问题是,系统没有调用第二个读取函数,它只是退出。这是我的代码的一小部分

SECTION .bss
        buffer resb 1
SECTION .text
    global _start
    _start:
    ...some unuseful and commented code
    call getUserAction ;reading 1 byte from console, works fine
    ...
    jmp count
    call exit
    count:
           ...
           call getUserAction; don't work, just exits from program without err
           ...commented code to debug
           call exit

    getUserAction: ;proc to read from console 1 byte
             mov eax, 3
             mov ebx, 0
             mov ecx, buffer
             mov edx, 1
             int 80h
             ret

此外,我试图将getUserAction中的代码放入“count”proc中,但它什么都没改变。

UPD:对第一条评论的回答: 在第二个“getUserAction”之后:

 mov eax, [buffer]
 cmp eax, 'u'    
 je setUsersValues
 cmp eax, 'e'
 je callFunc     
 call exit

UPD2 :我很抱歉,这是所有代码

%define newline 10,0
%define A1 -7
%define A2 3
%define A3 2
%define A4 4
%define A5 2
SECTION .data
    labInfo     db "============Lab_3============",newline
    labInfoLen  equ $ - labInfo
    mainMenu    db "Choose the ex:",newline,\
            " r - call count",newline,\
            " t - call beep", newline,\
            " y - call exit func",newline
    mainMenuLen equ $ - mainMenu
    funcStr     db "Here func func func", newline
    funcStrLen  equ $ - funcStr
    countPromt  db "Please,choose the variant of variables value",newline,\
              " u - user defined values", newline,\
              " e - execerciese defined values", newline,\
              "Your choise:"
    promtLen    equ $ - countPromt
SECTION .bss
    buffer resb 1
    resultValue resb 1
%macro calculateFunc 5
     push eax
     push edx
     push ecx
         mov eax, %1
         mov ecx, %2
         add eax, ecx

         mov ecx, %3
         imul ecx

         mov ecx, %4
         xor edx, edx
         idiv ecx

         mov ecx, %5
         add eax, ecx
         mov [resultValue], eax
    pop ecx
    pop edx
        pop eax
%endmacro
SECTION .text
global _start
_start:
    ;call showPromt
    push labInfo
    push labInfoLen
    call printStr
    add esp, 8
    ;call showVarsList
    push mainMenu
    push mainMenuLen
    call printStr
    add esp, 8

    call getUserAction
    ;get get get action
    mov eax, [buffer]
    cmp eax, 'r'
    je count
    cmp eax, 't'
    je beep
    cmp eax, 'y'
    je exit
    jmp _start
count:
    ;showFuncInfo
    push funcStr
    push funcStrLen
    call printStr
    add esp, 8
    ;showProposal
    push countPromt
    push promtLen
    call printStr
    add esp, 8

    call getUserAction
    mov eax, [buffer]
    cmp eax, 'u'    
    je setUsersValues
    cmp eax, 'e'
    je callFunc 
    call exit
    ret
setUsersValues:
    nop; add some code later
        ret
callFunc:
    calculateFunc A1,A2,A3,A4,A5
    add byte [resultValue], '0'
    push resultValue
    push 1
    call printStr ; print Result
    add esp, 8
    ret
printStr:
    push ebp
    mov ebp, esp

    mov eax, 4
    mov ebx, 1
    mov ecx, [ebp+12]
    mov edx, [ebp+8] 
    int 80h

    pop ebp
    ret 
getUserAction:
    mov eax, 3
    mov ebx, 0
    mov ecx, buffer
        mov edx, 1
    int 80h
    ret
beep:
     nop;add some code later
     ret
exit:
    mov eax, 1
    xor ebx, ebx
    int 80h

1 个答案:

答案 0 :(得分:0)

没有看到所有代码,你的缓冲区至少应该是2个字节。 sys_read和sys_write的大小应该是2而不是1。