做伪代码翻译成汇编语言时,遇到了如下错误:
li***@VirtualBox:~/lab2/assignment$ make run
>>> begin test
makefile:13: recipe for target 'run' failed
make: *** [run] 段错误 (core dumped)
在网上找了很多办法都解决不了,可以告诉我究竟是哪里出错了吗?
makefile文件内容如下:
TARTGET = test
AS = nasm
CXX = g++
ASM_FILE = $(wildcard *.asm)
CXX_FILE = $(wildcard *.cpp)
ALL_OBJ += $(ASM_FILE:%.asm=%.o)
ALL_OBJ += $(CXX_FILE:%.cpp=%.o)
run:
@rm -rf *.o
@nasm -f elf32 $(ASM_FILE)
@g++ -m32 -g -c $(CXX_FILE)
@g++ -m32 -o $(TARTGET) $(ALL_OBJ)
@./$(TARTGET)
clean:
@rm -rf *.o
我自己编写的汇编文件student.asm如下:
; If you meet compile error, try 'sudo apt install gcc-multilib g++-multilib' first
%include "head.include"
; you code here
.data:
string db 1024 dup(0)
count equ $-string
.code:
mov ecx,if_flag
mov ebx,string
your_if:
mov eax,a1
cmp eax,12
jnl ope1
mov ebx,2
idiv ebx
inc eax
jmp Exit
ope1:
cmp eax,24
jng ope2
mov edx,eax
neg eax
add eax,24
imul eax,edx
jmp Exit
ope2:
shl eax,4
jmp Exit
Exit:
ret
your_while:
mov ecx,a2
mov ebx,while_flag
Loop:
call my_random
mov [ebx+ecx-12],eax
sub ecx,1
cmp ecx,12
jge Loop
mov eax,ebx
ret
%include "end.include"
your_function:
mov edx,0
mov ebx,string
for:
cmp word [ebx+edx],'\0'
je exit
add edx,1
pushad
mov eax,[ebx+edx]
push eax
call print_a_char
pop eax
popad
jmp for
exit:
ret
测试文件test.cpp代码如下:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#define RAND_WIDTH 26
extern "C" void student_function();
extern "C" void your_function();
int if_flag, a1, a2;
const char *your_string = "Mr.Chen, students and TAs are the best!\n";
char *while_flag, *random_buffer;
// THE ONLY CODE YOU CAN CHANGE IN THIS FILE!!!
void student_setting() {
a1 = 24;
a2 = 14;
}
extern "C" char my_random() {
char c = rand() % (RAND_WIDTH) + 'a';
random_buffer[a2 - 12] = c;
return c;
}
extern "C" void print_a_char(char c) {
std::cout << c;
}
void test_failed() {
std::cout << ">>> test failed" << std::endl;
exit(0);
}
int main() {
student_setting();
// init
std::cout << ">>> begin test" << std::endl;
if_flag = 0;
if(a2 >= 12) {
while_flag = new char[a2 - 12 + 1];
random_buffer = new char[a2 - 12 + 1];
if(!while_flag || !random_buffer) {
std::cout << "test can not run" << std::endl;
exit(-1);
}
memset(while_flag, 0, a2 - 12 + 1);
memset(random_buffer, 0, a2 - 12 + 1);
}
student_function();
if(a1 < 12) {
if (if_flag != a1 / 2 + 1) {
test_failed();
}
} else if(a1 < 24) {
if (if_flag != (24 - a1) * a1) {
test_failed();
}
} else {
if (if_flag != a1 << 4) {
test_failed();
}
}
std::cout << ">>> if test pass!" << std::endl;
if(strcmp(while_flag, random_buffer) == 0 && a2 < 12) {
std::cout << ">>> while test pass!" << std::endl;
} else {
test_failed();
}
your_function();
}
head.include代码如下:
global student_function
global your_function
extern a1
extern a2
extern my_random
extern your_string
extern if_flag
extern while_flag
extern print_a_char
student_function:
pushad
end.include代码如下:
student_function_end:
popad
ret