程序集:小写到大写 [英] Assembly: lowercase to UPPERCASE
本文介绍了程序集:小写到大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将h4ppy c0d1ng"转换为H4PPY C0D1NG".我是这门语言的初学者,但这是我的尝试(ubuntu i386 VirtualBox Mac).我认为 int 21h 是错误的,除了程序在执行时不会完成也不会打印字符串:
I need to transform "h4ppy c0d1ng" into "H4PPY C0D1NG". I am a beginner in this language, but here is my attempt (ubuntu i386 VirtualBox Mac).I think the int 21h is wrong, other than that the program won't finish nor print the string when executed:
section .text
GLOBAL _start
_start:
mov ecx, string
mov edx, length
call toUpper
call print
mov eax, 1
mov ebx, 0
int 80h
;String in ecx and length in edx?
;-------------------------
toUpper:
mov eax,ecx
cmp al,0x0 ;check it's not the null terminating character?
je done
cmp al,'a'
jb next_please
cmp al,'z'
ja next_please
sub cl,0x20
ret
next_please:
inc al
jmp toUpper
done: int 21h ; just leave toUpper (not working)
print:
mov ebx, 1
mov eax, 4
int 80h
ret
section .data
string db "h4ppy c0d1ng", 10
length equ $-string
推荐答案
一些小改动,它应该可以运行:
some minor changes and it should run:
section .text
GLOBAL _start
_start: mov ecx, string
call toUpper
call print
mov eax,1
mov ebx,0
int 80h
toUpper:
mov al,[ecx] ; ecx is the pointer, so [ecx] the current char
cmp al,0x0
je done
cmp al,'a'
jb next_please
cmp al,'z'
ja next_please
sub al,0x20 ; move AL upper case and
mov [ecx],al ; write it back to string
next_please:
inc ecx ; not al, that's the character. ecx has to
; be increased, to point to next char
jmp toUpper
done: ret
print: mov ecx, string ; what to print
mov edx, len ; length of string to be printed
mov ebx, 1
mov eax, 4
int 80h
ret
section .data
string: db "h4ppy c0d1ng",10,0
len: equ $-string
更新打印"工作,
修正大写错误:al 保存字符,而不是 cl
添加了一个符号来确定字符串的长度
edit:
updated "print" to work,
bugfix for making uppercase: al holds the char, not cl
added a symbol to determine the length of the string
在我的 linux 机器上测试过,不行
tested on my linux box, not it works
这篇关于程序集:小写到大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文