Linux 使用阵列组装x86中的河内塔
大家好,我正试图在x86汇编中创建一个河内塔,但我正在尝试使用数组。所以这段代码从用户那里获取一个数字作为Linux中的参数,然后错误检查一堆东西。所以现在我只想做一个算法,使用我制作的三个数组(开始、结束、临时),并一步一步地输出它们。如果有人能帮忙,我们将不胜感激`Linux 使用阵列组装x86中的河内塔,linux,assembly,x86,nasm,towers-of-hanoi,Linux,Assembly,X86,Nasm,Towers Of Hanoi,大家好,我正试图在x86汇编中创建一个河内塔,但我正在尝试使用数组。所以这段代码从用户那里获取一个数字作为Linux中的参数,然后错误检查一堆东西。所以现在我只想做一个算法,使用我制作的三个数组(开始、结束、临时),并一步一步地输出它们。如果有人能帮忙,我们将不胜感激` %include "asm_io.inc" segment .data ; where all the predefined vari
%include "asm_io.inc"
segment .data ; where all the predefined variables are stored
aofr: db "Argument out of Range", 0 ; define aofr as a String "Argument out of Range"
ia: db "Incorrect Argument", 0 ; define ia as a String "Incorrect Argument"
tma: db "Too many Arguments", 0 ; define tma as a String "Too many Arguments"
hantowNumber dd 0 ; define hantowNumber this is what the size of the tower will be stored in
start: dd 0,0,0,0,0,0,0,0,9 ; this array is where all the rings start at
end: dd 0,0,0,0,0,0,0,0,9 ; this array is where all the rings end up at
temp: dd 0,0,0,0,0,0,0,0,9 ; this array is used to help move the rings
test: dd 0,0,0,0,0,0,0,0,9
; The next couple lines define the strings to show the pegs and what they look like
towerName: db " Tower 1 Tower 2 Tower 3 ", 10, 0
lastLineRow: db "XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX ", 10, 0
buffer: db " ", 0
fmt:db "%d",10,0
segment .bss ; where all the input variables are stored
segment .text
global asm_main ; run the main function
extern printf
asm_main:
enter 0,0 ; setup routine
pusha
mov edx, dword 0 ; set edx to zero this is where the hantowNumber is saved for now
mov ecx, dword[ebp+8] ; ecx has how many arguments are given
mov eax, dword[ebp+12] ; save the first argument in eax
add eax, 4 ; move the pointer to the main argument
mov ebx, dword[eax] ; save the number into ebx
push ebx ; reserve ebx
push ecx ; reserve ecx
cmp ecx, dword 2 ; compare if there are more the one argument given
jg TmA ; if more then one argument is given then jump Too many Argument (TmA)
mov ecx, 0 ; ecx = 0
movzx eax, byte[ebx+ecx] ; eax is the first character number from the inputed number
sub eax, 48 ; subtract 48 to get the actual number/letter/symbol
cmp eax, 10 ; check if eax is less then 10
jg IA ; if eax is greater then 10 then it is a letter or symbol
string_To_int: ; change String to int procedure
add edx, eax ; put the number in edx
inc ecx ; increase counter (ecx)
movzx eax, byte[ebx+ecx] ; move the next number in eax
cmp eax, 0 ; if eax = 0 then there are no more numbers
mov [hantowNumber], edx ; change hantowNumber to what ever is in edx
je rangeCheck ; go to rangeCheck to check if between 2-8
sub eax, 48 ; subtract 48 to get the actual number/letter/symbol
cmp eax, 10 ; check if eax is less then 10
jg IA ; if eax is greater then 10 then it is a letter or symbol
imul edx, 10 ; multiply edx by 10 so the next number can be added to the end
jmp string_To_int ; jump back to string_To_int if not done
rangeCheck: ; check the range of the number
cmp edx, dword 2 ; compare edx with 2
jl AofR ; if hantowNumber (edx) < 2 then go to Argument out of Range (AofR)
cmp edx, dword 8 ; compare edx with 8
jg AofR ; if hantowNumber (edx) > 8 then go to Argument out of Range (AofR)
mov ecx, [hantowNumber] ; move the number enterd by user in ecx
mov esi, 28 ; esi == 28 for stack pointer counter
setStart: ; set the first array the starting peg
mov [start+esi], ecx ; put ecx into the array
sub esi, 4 ; take one away from stack pointer conter
dec ecx ; take one away from ecx so the next number can go in to the array
cmp ecx, 0 ; compare ecx with 0
jne setStart ; if ecx != 0 then go to setStart loop
; This is the section where the algoritham should go for tower of hanoi
mov ecx, [hantowNumber]
towerAlgorithm:
cmp ecx, 0
jg Exit ; jump to Exit at the end of the program
dec ecx
IA:
mov eax, ia ; put the string in eax
push eax ; reserve eax
call print_string ; output the string that is in eax
call print_nl ; print a new line after the output
pop eax ; put eax back to normal
add esp, 4 ; takes 4 from stack
jmp Exit ; jump to Exit at the end of the program
AofR:
mov eax, aofr ; put the string in eax
push eax ; reserve eax
call print_string ; output the string that is in eax
call print_nl ; print a new line after the output
pop eax ; put eax back to normal
add esp, 4 ; takes 4 from stack
jmp Exit ; jump to Exit at the end of the program
TmA:
mov eax, tma ; put the string in eax
push eax ; reserve eax
call print_string ; output the string that is in eax
call print_nl ; print a new line after the output
pop eax ; put eax back to normal
add esp, 4 ; takes 4 from stack
jmp Exit ; jump to Exit at the end of the program
Exit: ; ends the program when it jumps to here
add esp, 9 ; takes 8 from stack
popa
mov eax, 0 ; return back to C
leave
ret
%包括“asm_io.inc”
数据段;存储所有预定义变量的位置
aofr:db“参数超出范围”,0;将aofr定义为字符串“参数超出范围”
ia:db“参数不正确”,0;将ia定义为字符串“不正确的参数”
tma:db“参数太多”,0;将tma定义为字符串“参数太多”
汉通号dd0;定义HantownNumber这是塔的大小将存储在其中
开始:dd 0,0,0,0,0,0,0,0,9;此数组是所有环的起点
结束:dd 0,0,0,0,0,0,0,0,9;这个数组是所有环结束的位置
温度:dd 0,0,0,0,0,0,0,0,9;此阵列用于帮助移动环
测试:dd 0,0,0,0,0,0,0,0,9
; 接下来的几行定义字符串,以显示桩及其外观
塔名:db“1号塔2号塔3号塔”,10,0
lastLineRow:db“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”,10,0
缓冲区:db“”,0
fmt:db“%d”,10,0
第三部分:bss;存储所有输入变量的位置
段.文本
全球asm_main;运行主函数
外部打印
asm_main:
输入0,0;设置程序
普沙
mov edx,dword 0;将edx设置为零这是暂时保存HantownNumber的位置
mov ecx,德沃德[ebp+8];ecx提供了多少个参数
mov eax,德沃德[ebp+12];在eax中保存第一个参数
添加eax,4;将指针移到主参数
mov-ebx,dword[eax];将号码保存到ebx中
推ebx;储备ebx
推动ecx;储备外汇
cmp ecx,dword 2;比较给出的一个参数是否有多个参数
jg-TmA;如果给出多个参数,则跳转太多参数(TmA)
mov-ecx,0;ecx=0
movzx-eax,字节[ebx+ecx];eax是输入的编号中的第一个字符编号
亚eax,48;减去48得到实际的数字/字母/符号
cmp-eax,10;检查eax是否小于10
jg-IA;如果eax大于10,则为字母或符号
字符串_到_int:;将字符串更改为int过程
添加edx、eax;将数字输入edx
ecx公司;增加计数器(ecx)
movzx-eax,字节[ebx+ecx];在eax中移动下一个数字
cmp-eax,0;如果eax=0,则没有更多的数字
mov[汉通编号],edx;将HanTownNumber更改为edx中的值
日本脑炎范围检查;转到Range check以检查是否介于2-8之间
亚eax,48;减去48得到实际的数字/字母/符号
cmp-eax,10;检查eax是否小于10
jg-IA;如果eax大于10,则为字母或符号
imul edx,10;将edx乘以10,以便将下一个数字加到末尾
jmp字符串_到_int;如果未完成,跳回字符串\u至\u int
范围检查:;检查数字的范围
cmp edx,dword 2;将edx与2进行比较
jl-AofR;如果HanTownNumber(edx)<2,则转到参数超出范围(AofR)
cmp edx,dword 8;将edx与8进行比较
jg-AofR;如果HantownNumber(edx)>8,则转到参数超出范围(AofR)
mov ecx,[汉通编号];移动用户在ecx中输入的号码
Movesi,28岁;堆栈指针计数器的esi==28
设置开始:;将第一个数组设置为起始桩
mov[start+esi],ecx;将ecx放入阵列中
亚esi,4;从堆栈指针内容中取出一个
dec-ecx;从ecx中取出一个,以便下一个数字可以进入阵列
cmp-ecx,0;将ecx与0进行比较
jne-setStart;如果ecx!=0,然后转到setStart循环
; 这是algoritham为ha塔设计的部分
enter 0,0
pusha
; address of 1st argument is on stack at address ebp+12
; address of 2nd arg = address of 1st arg + 4
mov eax, dword [ebp+12] ;eax = address of 1st arg
add eax, 4 ;eax = address of 2nd arg
mov ebx, dword [eax] ;ebx = 2nd arg, it is pointer to string
mov eax, 0 ;clear the register
mov al, [ebx] ;it moves only 1 byte
sub eax, '0' ;now eax contains the numeric value of the firstcharacter of string