使用nasm

时间:2016-04-29 01:24:13

标签: assembly io nasm i386

我试图从用户那里获得输入并将其写入文件 使用GHex我可以看到字符串存储正确但文件似乎存储为二进制文件(不能用gedit作为文本文件打开) 如何解决?

这是我的代码

section .data
CreateErrorPrompt:  db 'Error creating file',10
CreateErrorPromptSize:  equ $-CreateErrorPrompt
ArgCErrorPrompt:    db 'Please use $myeditor filename',10
ArgCErrorPromptSize:    equ $-ArgCErrorPrompt
inputBufferSize:    equ 84  ; the buffer size (in .bss section)

section .bss
inputBuffer:    resb 84 ; Buffer to read file in
filePointer:    resb 4  ; pointer for the file
fileDescriptor: resb 1  ; pointer for descriptor

section .text
global _start

_start:
nop

pop ebx     ; argc (argCount)
cmp ebx,2   ; make sure arguments are two
jne ArgCError   ; jump to ArgCError

pop ebx     ; name of the program
pop ebx     ; name of the file
cmp ebx,0   ; make sure entered falid file name
jbe ArgCError   ; print error if not valid
mov [filePointer],ebx   ; put file pointer

getInput:
mov eax,3   ; system call for read
mov ebx,0   ; read from stdin
mov ecx,inputBuffer ; read in input buffer
mov edx,inputBufferSize ; set input size
int 80h     ; call the kernel
jmp OpenTheFile

OpenTheFile:
mov eax,5   ; system call for open
mov ebx,[filePointer]   ; put file pointer
mov ecx,2   ; 0_RDWR
int 80h     ; call the kernel
cmp eax,0   ; check if opened succefully
jle CreateTheFile   ; Try to create
mov [fileDescriptor],eax    ; put file descriptor
jmp fileOpened

CreateTheFile:
mov eax,8   ; system call for creat
mov ebx,[filePointer]   ; put file pointer
mov ecx,0700    ; access rights -rwxr-xr-x
int 80h     ; call the kernel
cmp eax,0   ; check if file was created
jle errorCreating   ;error creating file
mov [fileDescriptor],eax    ; put file descriptor
jmp fileOpened

fileOpened:
writeToFile:
mov eax,4   ; system call for write
mov ebx,[fileDescriptor]    ; put file descriptor
mov ecx,inputBuffer ; write buffer
mov edx,inputBufferSize ; the size
int 80h     ; call the kernel
mov eax,36      ;sys_sync
int 80h
jmp CloseBeforeExit ; exit

errorCreating:
mov eax,4   ; system call for write
mov ebx,1   ; stdout
mov ecx,CreateErrorPrompt   ; offset to write
mov edx,CreateErrorPromptSize   ; length to be written
int 80h
jmp ExitProgram ; exit

ArgCError:
mov eax,4   ; system call for write
mov ebx,1   ; stdout
mov ecx,ArgCErrorPrompt ; offset to write
mov edx,ArgCErrorPromptSize ; length to be written
int 80h
jmp ExitProgram ; exit

CloseBeforeExit:
mov eax,6 ; system call for close
mov ebx,[fileDescriptor]    ; put file descriptor
int 80h     ; call the kernel
ExitProgram:
mov eax,1   ; The system call for exit (sys_exit)
mov ebx,0   ; Exit with return code of 0 (no error)
int 80h

0 个答案:

没有答案