- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试编写一个程序,将小写字符字符串转换为大写字符,使用缓冲区存储初始字符串。我遇到的问题是我的程序将打印出一个无限循环的字符,这些字符必须与我给它的字符串相似。
我认为代码中存在的其他问题如下:
一些子例程在调用结束时使用ret
。我遇到的问题是弄清楚这些子例程中的哪些实际上不需要ret
,最好与jmp
一起使用.老实说,我在这里对两者的语义有点困惑。例如,使用 ja
调用的子例程是否需要在调用结束时被ret
?
我还试图打印出在用于转换值的循环的每次迭代中发生的迭代次数。无论出于何种原因,我将 inc
计数器并决定使用 PrintNumIter
例程打印它,遗憾的是,该例程没有做任何事情。
完整程序如下。
编码器
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
最佳答案
您的问题直接归因于这样一个事实,即您在处理完字符串缓冲区后永远不会将 nul 终止符附加到字符串缓冲区的末尾(据我所知, read
系统调用不会读回一个空值)。
不幸的是,由于你奇怪的控制流程,这有点难做,但改变 SetupBuf
应该可以解决问题(注意,你应该检查你没有溢出 buf
,但是对于 1KB,我怀疑你需要为学习程序担心):
SetupBuf:
mov ecx, esi
mov ebp, buf
mov [ebp+ecx],0 ;make sure the string is nul terminated
dec ebp
ret
注意
关于另一个似乎困扰您的代码的问题(您已经恰本地注意到),您的奇怪控制流。如此简单的指南(注意:不是规则,只是指南)希望能帮助您减少意大利面条式代码:
JMP
(和条件跳转)只能用于在同一过程中转到标签,否则您将开始陷入绑定(bind),因为您无法撤回。您唯一可以使用跳转的其他时间是尾调用,但在这个阶段您不应该担心,它更容易混淆。
当您要转到另一个过程时,请始终使用 CALL
,这允许您使用 RETN
/RET< 正确返回调用站点
指令,使控制流程更符合逻辑。
一个简单的例子:
print_num: ;PROC: num to print in ecx, ecx is caller preserved
push ecx
push num_format ; "%d\n"
call _printf
sub esp,8 ;cleanup for printf
retn
print_loop_count: ;PROC: takes no args
mov ecx,0x10 ;loop 16 times
do_loop: ;LABEL: used as a jump target for the loop
;good idea to prefix jump lables with "." to differentiate them
push ecx ;save ecx
call print_num ;value to print is already in ecx
pop ecx ;restore ecx
dec ecx
jnz do_loop ;again?
retn
关于linux - 输入一串字符并输出为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11303336/
请帮助我..我被困在这里.. 我真正想要的是检查密码是否重复单个字符或数字。 重复要求 aaaa = 假, abbb = 假 abag = 假 a33f = 假 abcd1234 = 真 一个字符在密
在 Ant 中,我有一个名为 ' some_property 的属性。 ',假设它的值为“hello”。 我正在尝试 替换文本文件中的占位符 将此属性的值(“ hello ”)作为 大写 . 所以,我
var temp=["dy34","fd","FD","av","AV","12esu",1,"DY34",1011,123,101]; 当我对数组进行排序时,我得到的结果是: result = [1
var font_name = $(".font").val(); 我的 JavaScript 代码中有这个。在我的 html 中,我有一个带有 .font 类的输入表单。 我想将 .font 中每个
有人可以解释一下我如何实现文本转换:大写; sIFR 3 的功能? 在文档上,它解释了如何使用它,但我真的不知道如何实现它。有人可以举个例子吗? Specifes text transformatio
这个问题已经有答案了: String contains - ignore case [duplicate] (5 个回答) 已关闭 3 年前。 我需要定义我的查询是 DDL 还是 DML。为此,我需要
所以我一直在用 Java 编写一小段代码,它从用户那里获取输入,计算大写字母、小写字母和其他部分(例如空格、数字,甚至括号),然后返回每个部分的数量用户。 我遇到的问题是,如果我输入“Hello Th
在土耳其语中,有两个 i 无点:ı,I 虚线:i,© 问题:每次我将 i 大写时,我得到的是 I。 当我将 i 大写时,我想得到一个 İ(仅限土耳其语),以及一个I 当我将 ı 大写时。 我有一个函数
使用点符号向数组添加属性是否会将其更改为对象? var arr = []; arr.something = "test"; 是数组吗? 我不这么认为,但 underscore.js 说是 consol
我希望 TextBlock、Label、MenuItem.Header 中的所有文本都以大写显示。字符串取自 ResourceDictionary 例如: 等等。 (也适用于 Label 和其他控
我正在尝试计算包含用户定义范围内所有大写字符的单元格实例的数量,我已经有一些代码可以循环并正确突出显示这些大写单元格,但我正在努力应用该逻辑到VBA 的 Countif 函数。这是我得到的代码,但它给
我正在使用 dplyr 进行数据清理。 我想做的一件事是将某些列中的值大写。 data$surname john Mary John mary ...
为什么低于一个不起作用?我需要转换 IsValue转换成大写值,然后需要用 NO 检查它值(value)。我该怎么做? {{item.IsValue}} 最佳答案 确保 uppercase在比较
我需要将相同的变量转换为大写|小写|大写。 /** * @package ${1 default="Hello"} * @subpackage ${com}_${1 capitalize
显然,拉丁字母表也是如此。但我是在概念上提出这个问题,跨越语言和 Unicode 规范。 实际上,这是为了比较两个字符串。如果你已经知道它们的字节数不同——在所有语言中——你能认为这足以保证它们不是同
今天我更新了我的 Octopress 博客,当我运行时: rake new_post["This is a test of title"] 它在 source/_post/2013-02-18-thi
除了大写部分之外,我的程序正在运行:以下是将英语单词 englishWord 翻译为 Pig 拉丁语单词 pigLatinWord 的方法:A。如果英语单词中没有元音,那么pigLatinWord就是
通过使用一个输入文本框,输入类型只允许字母。输入的值为'a',它应该在文本框外显示为'A'? 如果我们在输入文本中输入小写字母“a”,那么它会希望在框外显示大写字母“A”...以下是我的html代码:
我正在开发一个特殊的脚本来修复文本区域内的字母。我在堆叠时发现了一个问题。在 Stackoverflow 上,我找不到解决方案,需要帮助。 我的脚本有一个系统,用于识别每个以大写首字母开头的单词,以及
我在 bash 中有以下行: echo "Manufacturer: $(echo ${family:-$name}|cut -d' ' -f1)" 我想使用 ${var^} 语法将回显字符串大写,但
我是一名优秀的程序员,十分优秀!