【发布时间】:2020-10-16 21:33:40
【问题描述】:
这实际上是我今天关于这个特殊问题的第二个问题,但另一个问题很快就得到了回答。
本质上,我试图接收一串字母(没有数字或符号),然后将每个输入的字母与代表北约军事音标(Alpha、Bravo、Charlie、等)并输出与该字母等效的军事代表。
这就是我卡住的地方。我对组装相当陌生,这是一项家庭作业,因此非常需要和感谢帮助。我的教授不擅长提供资源来学习这些东西,而且很难在网上找到解决确切问题的好资源。
任何帮助将不胜感激。具体如何比较每个字母输入到数组。我已经成功地将输入存储在一个变量中。
下面是我正在尝试做的 C# 表示。
class MilAlpha
{
static void Main(string[] args)
{
string input;
string[] miliAlpha = { "Alpha", "Beta", "Charlie", "Delta", "Echo", "Foxtrot", "Golf",
"Hotel", "India", "Juliet", "Kilo", "Lima", "Mike", "November",
"Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform",
"Victor", "Whiskey", "X-Ray", "Yankee", "Zulu" };
Console.WriteLine("Enter a string of text: ");
input = Console.ReadLine();
for (int i = 0; i < input.Length; i++) {
for (int j = 0; j < miliAlpha.Length; j++) {
if (input[i] == ' ')
Console.WriteLine("\n")
string temp = miliAlpha[j].ToLower();
if (input[i] == temp[0])
Console.WriteLine("\n" + miliAlpha[j] + "\n");
}
}
Console.ReadKey();
}
}
编辑:
所以我相信这应该做我想做的事,但它似乎没有按预期工作。它在调试器中比较正确的东西,但是当它打印数组的相应部分时,它根本不打印任何东西。
.section .data
MAlpha:
.asciz "Alpha \n"
.equ ElementLen, .-MAlpha
.asciz "Bravo \n"
.asciz "Charlie \n"
.asciz "Delta \n"
.asciz "Echo \n"
.asciz "Foxtrot \n"
.asciz "Golf \n"
.asciz "Hotel \n"
.asciz "India \n"
.asciz "Juliet \n"
.asciz "Kilo \n"
.asciz "Lima \n"
.asciz "Mike \n"
.asciz "November \n"
.asciz "Oscar \n"
.asciz "Papa \n"
.asciz "Quebec \n"
.asciz "Romeo \n"
.asciz "Sierra \n"
.asciz "Tango \n"
.asciz "Uniform \n"
.asciz "Victor \n"
.asciz "Whiskey \n"
.asciz "X-Ray \n"
.asciz "Yankee \n"
.asciz "Zulu \n"
.asciz " \n"
.equ MAlphaLen, .-MAlpha
Input:
.fill 80
.equ InputLen, .-Input
InputMSG:
.ascii "Please enter a word: "
.equ InputMSGLen, .-InputMSG
BlankLine:
.ascii "\n"
.equ BlankLineLen, .-BlankLine
Converting:
.ascii "\nConverting to NATO Alphabet...\n\n"
.equ ConvertingLen, .-Converting
.section .bss
.section .text
.globl _start
GetInput:
movl $4, %eax
movl $1, %ebx
movl $InputMSG, %ecx
movl $InputMSGLen, %edx
int $0x80
movl $3, %eax
movl $0, %ebx
movl $Input, %ecx
movl $InputLen, %edx
int $0x80
ret
PrintInput:
movl $4, %eax
movl $1, %ebx
movl $BlankLine, %ecx
movl $BlankLineLen, %edx
int $0x80
movl $4, %eax
movl $1, %ebx
movl $Input, %ecx
movl $InputLen, %edx
int $0x80
movl $4, %eax
movl $1, %ebx
movl $Converting, %ecx
movl $ConvertingLen, %edx
int $0x80
ret
Convert:
# Get first letter of input string
# Compare letter to first letter of each array entry
# When match is found, print Array entry to screen
# Repeat until end of input string
movl $Input, %eax
movl $MAlpha, %edi
call Loop
ret
Loop:
movb (%eax), %al
cmp $0x0A, %al
je Finished
call CompareAlpha
jmp Loop
CompareAlpha:
movb (%edi), %bl
cmpb %bl, %al
je PrintWord
addl $ElementLen, %edi
jmp CompareAlpha
PrintWord:
movl $4, %eax
movl $1, %ebx
movl (%edi), %eax
movl $ElementLen, %edx
int $0x80
Finished:
call ExitProg
_start:
call GetInput
call PrintInput
call Convert
call ExitProg
PrintMAlpha:
movl $4, %eax
movl $1, %ebx
movl $MAlpha, %ecx
movl $MAlphaLen, %edx
int $0x80
ExitProg:
movl $1, %eax
movl $0, %ebx
int $0x80
【问题讨论】:
-
我认为“比较”的想法会误导你。明智的做法是使用查找表,使用字母索引到字符串数组(使用适当的边界检查);您永远不会将字母与字符串进行比较。尝试先用 C 等高级语言编写它,然后您可能会更好地了解该做什么。
-
但总的来说,Stack Overflow 期望问题比这更集中。这个网站并不是真正为“我如何开始”问题或家庭作业帮助而设置的。这往往需要与某人进行长时间的反复交流来帮助您,而 Stack Overflow 是围绕可以提出、回答和完成的问题而构建的。
-
@NateEldredge 那么还有一个更具体的问题,如何将数组用作查找表?我是一个相当有经验的高级程序员,但是汇编对我来说是全新的,即使按照你的建议做了我也遇到了麻烦(我是在别人的建议下做的,很有趣)。
-
那么把你的 C 代码放在问题中怎么样?或许还有你迄今为止编写的任何骨架汇编代码?
-
最好知道您是在编写 32 位还是 64 位代码,以及您将使用什么操作系统来组装和运行您的代码(这决定了要使用的适当代码模型) .
标签: linux assembly linux-kernel gnu 32-bit