【发布时间】:2012-07-03 01:27:00
【问题描述】:
我正在尝试编写一个程序,它将小写字符串转换为大写,使用缓冲区来存储初始字符串。我遇到的问题是我的程序将打印出一个无限循环的字符,这些字符必须与我给它的字符串相似。
我认为代码中存在的其他问题如下:
一些子例程在调用结束时使用
ret。我遇到的问题是找出这些子例程中的哪些 实际上不需要ret,最好与jmp一起使用。老实说,我在这两者的语义之间有点困惑。例如,使用ja调用的子例程是否需要在调用结束时进行ret'ed?我还试图打印出用于转换值的循环的每次迭代中发生的迭代次数。无论出于何种原因,我都会
inc计数器并决定使用PrintNumIter例程打印它,可惜的是,它没有做任何事情。
完整的程序如下。
Codez
bits 32
[section .bss]
buf: resb 1024 ;allocate 1024 bytes of memory to buf
[section .data]
;*************
;* CONSTANTS *
;*************
;ASCII comparison/conversion
LowercaseA: equ 0x61
LowercaseZ: equ 0x7A
SubToUppercase: equ 0x20
;IO specifiers/descriptors
EOF: equ 0x0
sys_read: equ 0x3
sys_write: equ 0x4
stdin: equ 0x0
stdout: equ 0x1
stderr: equ 0x2
;Kernel Commands/Program Directives
_exit: equ 0x1
exit_success: equ 0x0
execute_cmd: equ 0x80
;Memory Usage
buflen: equ 0x400 ;1KB of memory
;*****************
;* NON-CONSTANTS *
;*****************
iteration_count: db 0
query : db "Please enter a string of lowercase characters, and I will output them for you in uppercase ^.^: ", 10
querylen : equ $-query
[section .text]
global _start
;===========================================
; Entry Point
;===========================================
_start:
nop ;keep GDB from complaining
call AskUser
call Read
call SetupBuf
call Scan
call Write
jmp Exit
;===========================================
; IO Instructions
;===========================================
Read:
mov eax, sys_read ;we're going to read in something
mov ebx, stdin ;where we obtain this is from stdin
mov ecx, buf ;read data into buf
mov edx, buflen ;amount of data to read
int execute_cmd ;invoke kernel to do its bidding
ret
Write:
mov eax, sys_write ;we're going to write something
mov ebx, stdout ;where we output this is going to be in stdout
mov ecx, buf ;buf goes into ecx; thus, whatever is in ecx gets written out to
mov edx, buflen ;write the entire buf
int execute_cmd ;invoke kernel to do its bidding
ret
AskUser:
mov eax, sys_write
mov ebx, stdout
mov ecx, query
mov edx, querylen
int execute_cmd
ret
PrintNumIter:
mov eax, sys_write
mov ebx, stdout
push ecx ;save ecx's address
mov ecx, iteration_count ;print the value of iteration_count
mov edx, 4 ;print 4 bytes of data
int execute_cmd
pop ecx ;grab the value back in
ret
;===========================================
; Program Preperation
;===========================================
SetupBuf:
mov ecx, esi ;place the number of bytes read into ecx
mov ebp, buf ;place the address of buf into ebp
dec ebp ;decrement buf by 1 to prevent "off by one" error
ret
;===========================================
; Conversion Routines
;===========================================
ToUpper:
sub dword [ebp + ecx], SubToLowercase ;grab the address of buf and sub its value to create uppercase character
Scan:
call PrintNumIter ;print the current iteration within the loop
cmp dword [ebp + ecx], LowercaseA ;Test input char against lowercase 'a'
jb ToUpper ;If below 'a' in ASCII, then is not lowercase - goto ToLower
cmp dword [ebp + ecx], LowercaseZ ;Test input char against lowercase 'z'
ja ToUpper ;If above 'z' in ASCII, then is not lowercase - goto ToLower
dec ecx ;decrement ecx by one, so we can get the next character
inc byte [iteration_count] ;increment the __value__ in iteration count by 1
jnz Scan ;if ecx != 0, then continue the process
ret
;===========================================
;Next:
; dec ecx ;decrement ecx by one
; jnz Scan ;if ecx != 0 scan
; ret
;===========================================
Exit:
mov eax, _exit
mov ebx, exit_success
int execute_cmd
【问题讨论】:
-
您应该使用调试器单步执行您的程序,以找出其行为与您的预期不同的地方。
-
调试了很多;我刚刚开始学习 x86 汇编。一直在检查寄存器和地址等等。你很了解asm吗?
-
如果您已经单步执行了代码,大概您发现了一个(或多个)特定行不符合您的预期/预期?
-
是的,我有。问题是它不像剖析一个函数并简单地“看到”发生了什么那么简单。装配是另一种野兽。
-
我真的不知道这是否会有所帮助,但我个人使用
jmp转到另一个标签(在您的情况下,Read:、Write:等,而不是 @ 987654332@ 至_start:)。我非常怀疑这是问题所在,但这是我的两分钱。 ;)
标签: linux assembly io x86 nasm