【问题标题】:Converting from c name sorting code to NASM assembly code crashes从 c 名称排序代码转换为 NASM 汇编代码崩溃
【发布时间】:2015-12-04 18:59:28
【问题描述】:

我正在尝试将给定 txt 文件中对名称进行排序的给定 c 代码转换为 NASM 汇编代码。我逐行实现了c代码,它确实没有错误地构建;但是,当我运行它时,它崩溃了。 这是用于对 .txt 文件 'names.txt' 中的名称进行排序的工作 c 代码:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  char path[]="names.txt";

  char szName[50];

  int count=0;

  int size=0;

  int x=0;
  int y=0;

  char* temp=0;
  int pos=0;

  FILE* file=fopen(path,"rt");

  if (file){

  while (!feof(file)){

  fscanf(file,"%50s",szName);
  count++;


  }

  }
  else{
       printf("Error opening file\n");
       return 1;
  }

  printf ("Count: %d\n",count);

  char** names=(char**)malloc(count*sizeof(char*));

  rewind(file);

  count=0;



  while (!feof(file)){

  fscanf(file,"%50s",szName);

  size=strlen(szName);

  names[count]=(char*)malloc(size+1);

  strcpy(names[count],szName);

  count++;


  }

  printf("Original file\n");

  for (x=0;x<count;x++){

  printf("Name %d:\t%s\n",x+1,names[x]);

  }


  for (x=0;x<count;x++){

  temp=names[x];
  pos=x;

  for (y=x;y<count;y++){

      if (strcmp(temp,names[y])>0){

      temp=names[y];
      pos=y;

      }    
  }

  temp=names[x];
  names[x]=names[pos];
  names[pos]=temp;    
  }

  printf("Sorted names\n");

  for (x=0;x<count;x++){

  printf("Name %d:\t%s\n",x+1,names[x]);

  }

  system("PAUSE");  
  return 0;
}

这是我在 NASM 程序集中的这个 c 代码的实现:

extern _scanf
extern _printf
extern _fopen
extern _rewind
extern _fscanf
extern _fprintf
extern _malloc
extern _free
extern _strcpy
extern _strcmp
extern _feof
extern _strlen
extern _system
extern _fclose

segment .data

szFname db 'names.txt',0
szName TIMES 50 db 0
szMode db 'rt',0
szIn db '%49s',0
szOut db 'Name %d: %s',10,0
szError db 'Could not open file: %s',10,0
szCount db 'Count: %d',10,0
szPause db 'PAUSE',0
szOriginal db 'Original file\n',0
szSorted db 'Sorted names\n',0


segment .code

global _main
_main:

%define count [ebp-4]
%define size [ebp-8]
%define x [ebp-12]
%define y [ebp-16]
%define temp [ebp-20]
%define pos [ebp-24]
%define file [ebp-28]
%define names [ebp-32]

enter 32,0
;int main(int argc, char *argv[]]

;charpath[]="names.txt";

;char szName[50];

;int count=0; 
;int size=0;
;int x=0;
;int y=0;
;char* temp=0;
;int pos=0;
mov dword count,0
mov dword size,0
mov dword x,0
mov dword y,0 
mov dword temp,0
mov dword pos,0

;FILE* file=fopen(path,"rt");
push dword szMode
push szFname ;PATH
call _fopen
add esp, 8
mov dword file, eax


;if (file){
cmp dword file,0 ;NO WAY IN HELL THIS IS RIGHT
jne _noFile

; while (!feof(file)){
_while1begin:

push dword file  
call _feof
add esp,4
cmp eax,0
je _while1end

;  fscanf(file,"%50s",szName);
push dword szName
push dword szIn
push dword file
call _fscanf
add esp,12

;  count++;}}
inc dword count
jmp _while1begin

;else{
; printf("Error opening file\n");
_noFile:
push dword szError
call _printf
add esp,4

_while1end:

;printf ("Count: %d\n",count);
push dword count
push dword szCount
call _printf
add esp,8

;char** names=(char**)malloc(count*sizeof(char*));

;rewind(file);
push dword file
call _rewind
add esp,4

;count=0;
mov dword count,0

;while (!feof(file)){
_while2begin:
push dword file  
call _feof
add esp,4
cmp eax,0
je _while2end

; fscanf(file,"%50s",szName);
push dword szName
push dword szIn
push dword file
call _fscanf
add esp, 12

; size=strlen(szName);
push dword szName
call _strlen
add esp,4
mov dword size,eax

; names[count]=(char*)malloc(size+1);
mov ebx, size
inc ebx
push ebx
call _malloc
add esp,4
mov ebx, dword names
mov ecx, dword count
mov [ebx+ecx], eax

; strcpy(names[count],szName);
mov ebx, dword names
mov ecx, dword count
push dword szName
push dword [ebx+ecx]
call _strcpy
add esp,8


; count++;}
inc dword count
jmp _while2begin

_while2end:

;printf("Original file\n");
push dword szOriginal
call _printf
add esp,4

;for (x=0;x<count;x++){
mov dword x, 0
_for1begin:
mov ebx, dword x
mov ecx, dword count
cmp ebx, ecx
jge _for1end
; printf("Name %d:\t%s\n",x+1,names[x]);}
mov ebx, dword names
mov ecx, dword count
mov eax, [ebx+ecx]

push eax
mov ebx,x
inc ebx
push ebx
push dword szOut
call _printf
add esp,12

inc dword count
jmp _for1begin

_for1end:  
mov dword x, 0
;for (x=0;x<count;x++){
_for2begin:
mov ebx, dword x
mov ecx, dword count
cmp ebx, ecx
jge _for2end
; temp=names[x];
mov ebx, dword x
mov ecx, dword names
lea eax, [ebx+ecx]
mov temp, eax
; pos=x;
mov dword pos, ebx
; y=x;
mov dword y, ebx

; while (y<count){
_while3begin:
mov ebx, dword y
mov ecx, dword count
cmp ebx, ecx
jge _while3end

;  if (strcmp(temp,names[y])>0){
mov ebx, dword y
mov ecx, dword names
mov edx, [ebx+ecx]
push edx
call _strcmp
add esp,4
cmp eax, 0  
jle _if1end  
;   temp=names[y];
mov ebx, dword y
mov ecx, dword names
lea eax, [ebx+ecx]
mov temp, eax
;   pos=y;}
mov dword pos, ebx
_if1end:
;   y++}
inc dword y
_while3end:

; temp=names[x];
mov ebx, dword x
mov ecx, dword names
lea eax, [ebx+ecx]
mov temp, eax
; names[x]=names[pos];
mov ebx, dword pos
mov ecx, dword names
mov eax, [ebx+ecx]

mov ebx, dword x
mov ecx, dword names
mov [ebx+ecx], eax
; names[pos]=temp;}
mov ebx, dword pos
mov ecx, dword names
mov eax, temp
mov dword [ebx+ecx], eax
_for2end:

;printf("Sorted names\n");
push dword szSorted
call _printf
add esp,4

;x=0;
mov dword x,0
;while (x<count){
_while4begin:

mov ebx, dword x
mov ecx, dword count
cmp ebx,ecx
jge _while4end

; printf("Name %d:\t%s\n",x+1,names[x]);
mov ebx, dword x
mov ecx, dword names
push dword [ebx+ecx]
push dword [ebx+1]
push dword szOut
call _printf
add esp,12
; x++}
inc dword x

_while4end:

;system("PAUSE");
push dword szPause
call_system
add esp,4


leave
ret

【问题讨论】:

  • 您有问题吗?
  • “我实现了 c 代码,......它确实没有错误地构建;......它崩溃了。”似乎与“这是工作的 c 代码”有点正交——除非代码的目的是崩溃......它要么是工作代码,要么不是。

标签: c arrays sorting nasm


【解决方案1】:

你忘了malloc 你的names 数组;实施

;char** names=(char**)malloc(count*sizeof(char*));

另外,count 以1 递增,但数组条目是指针大小的;访问 names 数组时,您需要将 count 乘以 4。更改此块:

; strcpy(names[count],szName);
mov ebx, dword names
mov ecx, dword count
push dword szName
push dword [ebx+ecx]              
call _strcpy
add esp,8

到

mov ebx, dword names
mov ecx, dword count
lea ebx, [ebx + ecx * 4]
push dword szName
push dword [ebx]
call _strcpy
add esp,8

不过可能还有其他问题..

【讨论】:

  • 谢谢你,但它仍然崩溃。显然更多的东西是错误的。
  • 你实现得很快 ;-) 现在是调试时间 - 简化你的代码:首先打开和关闭文件,注释掉中间的内容。然后逐步重新启用部分以找出导致问题的原因。或者也许使用调试器?
猜你喜欢
  • 2014-09-01
  • 1970-01-01
  • 2020-11-04
  • 2011-05-04
  • 2020-07-28
  • 1970-01-01
  • 2013-01-22
  • 2021-08-16
  • 1970-01-01
相关资源
最近更新 更多