c–NASM猜测数字游戏错了

c–NASM猜测数字游戏错了,第1张

概述我决定创建一个使用Linux系统调用的简单猜测数字游戏,以及一些C函数来提供更简单的界面.当我将int转换为字符串并在屏幕上打印正确答案时,我似乎遇到了分段错误.这是输出:Enter A Number One Through Ten:' : 3 Response did not match! The Answer Is:Segmentation fault

我决定创建一个使用Linux系统调用的简单猜测数字游戏,以及一些C函数来提供更简单的界面.当我将int转换为字符串并在屏幕上打印正确答案时,我似乎遇到了分段错误.

这是输出:

Enter A Number One Through Ten:" : 3Response dID not match! The Answer Is:Segmentation fault

这是C代码:

// print.c#include "/usr/include/stdio.h" #include "/usr/include/string.h"#include "/usr/include/stdlib.h"#include "/usr/include/time.h"voID print(const char* msg){    printf(msg);    return;}int compare(const char* str,const char* str2){    int i = strcmp(str,str2);    if (i == 0)    {        return 1;    }    else    {       return 0;    }}int divIDe(int num,int dem){    if (dem == 0)    {        printf("Undefined");        return 0;    }    else {        return (num / dem);    }}int randnum(int maxn){    if (maxn == 0)    {        maxn = 1;    }    srand(time(0));    return rand() % maxn;}int stoi(const char* str){    return atoi("str");}voID itos(int n){     char* buf = "5";     int ret = sprintf(buf,"%i\n",n);     if (ret == -1){    printf("Error!");    return;     }     else{    printf(buf);     }     return;}

这是NASM代码:

      ; Declared C functions.        extern print         extern compare        extern divIDe        extern randnum        extern stoi        extern itos        section .data             msg: db 'Enter A Number One Through Ten:" : ',10            ml: equ $- msg            t: db 'Response dID match!',10            tl: equ $- t            f: db 'Response dID not match! The Answer Is:',0            fl: equ $- f            str2: db 'Hello'        section .bss            ;srnum: resb 255            snum: resb 255            rnum: resb 255            num: resb 255        section .text            global _start ; Entry point function or label.        _start:            ; System call sys_write            mov eax,4            mov ebx,1            mov ecx,msg            mov edx,ml            int 80h        ; System call sys_read        mov eax,3        mov ebx,0        mov ecx,snum        mov edx,255        int 80h        ; Call stoi which converts string to int (parameter 1: is string to convert).        push snum        call stoi        mov [num],eax        mov ecx,esp        sub ecx,4        mov esp,ecx        ; Call random        push 10        call randnum        mov [rnum],eax        mov ecx,ecx        ; Compare the two integers.        mov eax,num        cmp eax,[rnum]        je true        jne false    true:        ; Call sys_write         mov eax,4        mov ebx,1        mov ecx,t        mov edx,tl        int 80h    false: ; Segmentation fault is somewhere in this label         mov eax,f        mov edx,fl        int 80h        push rnum        call itos         ; Calling sys_exit with exit code (0 = ERROR_SUCCESS)        mov eax,1        mov ebx,0        int 80h
最佳答案这段代码有问题:

char* buf = "5";int ret = sprintf(buf,n);

buf是指向只读内存的指针,sprintf希望能够修改其内容.
你应该将buf更改为一个数组:char buf [20](或20之外的任意数字,任意大到足以容纳你想要的内容) 总结

以上是内存溢出为你收集整理的c – NASM猜测数字游戏错了全部内容,希望文章能够帮你解决c – NASM猜测数字游戏错了所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1046879.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存