鱼C论坛

 找回密码
 立即注册
查看: 109|回复: 3

求助小程序

[复制链接]
发表于 2024-4-8 15:25:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我需要建立一个area.asm 文件。里面有一个function 叫 area。 需要运算出圆的面积,派 = 22/7 。
里面会调用一个lib文件extern libPuhfessorP, 来得到用户输入半径。
call libPuhfessorP
mov [INPUTTED_LONG], rax

面积按整数 和 非整数n/7
例如用户输入: 6  
最终实现:

This area function is fondly brought to you by Bob Dobalina.
Please enter the radius of a circle in whole number of meters: 6
The number 6 was received.
The area of a circle with this radius is 113 and 1/7 square meters.
The integer part of the area will be returned to the main program. Please enjoy
your circles.

请问大神们这样的程序怎么写?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-8 15:26:48 | 显示全部楼层
首先,你需要确保你使用的是汇编语言来编写该程序。你可以使用汇编器(如NASM)来将你的代码转换成可执行文件。

下面是一个示例的area.asm文件的框架,其中包含了你描述的功能:

  1. section .data
  2.     ; 定义字符串常量
  3.     message db "This area function is fondly brought to you by Bob Dobalina.", 0
  4.     prompt db "Please enter the radius of a circle in whole number of meters: ", 0
  5.     received db "The number %d was received.", 0
  6.     result db "The area of a circle with this radius is %d and 1/7 square meters.", 0
  7.     goodbye db "The integer part of the area will be returned to the main program. Please enjoy your circles.", 0

  8. section .bss
  9.     radius resb 4   ; 分配4个字节的空间用于存储用户输入的半径

  10. section .text
  11.     extern libPuhfessorP    ; 引入外部库函数

  12. global _start
  13. _start:
  14.     ; 显示欢迎消息
  15.     mov eax, 4
  16.     mov ebx, 1
  17.     mov ecx, message
  18.     mov edx, message_len
  19.     int 0x80

  20.     ; 提示用户输入半径
  21.     mov eax, 4
  22.     mov ebx, 1
  23.     mov ecx, prompt
  24.     mov edx, prompt_len
  25.     int 0x80

  26.     ; 调用外部库函数获取用户输入的半径
  27.     call libPuhfessorP
  28.     mov [radius], eax   ; 将半径存储到radius变量中

  29.     ; 显示接收到的半径
  30.     mov eax, 4
  31.     mov ebx, 1
  32.     mov ecx, received
  33.     mov edx, received_len
  34.     push dword [radius]
  35.     call printf
  36.     add esp, 4

  37.     ; 计算圆的面积
  38.     mov eax, [radius]
  39.     imul eax, eax      ; 半径的平方
  40.     imul eax, 22       ; 倍增22
  41.     mov edx, 7
  42.     idiv edx           ; 进行整除和取余
  43.     mov ebx, eax       ; 结果存储在ebx寄存器中

  44.     ; 显示圆的面积
  45.     mov eax, 4
  46.     mov ebx, 1
  47.     mov ecx, result
  48.     mov edx, result_len
  49.     push ebx
  50.     call printf
  51.     add esp, 4

  52.     ; 显示结束语
  53.     mov eax, 4
  54.     mov ebx, 1
  55.     mov ecx, goodbye
  56.     mov edx, goodbye_len
  57.     int 0x80

  58.     ; 返回给主程序
  59.     mov eax, 1
  60.     xor ebx, ebx
  61.     int 0x80

  62. section .data
  63. message db "This area function is fondly brought to you by Bob Dobalina.", 0
  64. prompt db "Please enter the radius of a circle in whole number of meters: ", 0
  65. received db "The number %d was received.", 10, 0
  66. result db "The area of a circle with this radius is %d and 1/7 square meters.", 10, 0
  67. goodbye db "The integer part of the area will be returned to the main program. Please enjoy your circles.", 10, 0

  68. section .bss
  69. radius resb 4   ; 分配4个字节的空间用于存储用户输入的半径

  70. section .text
  71. extern printf
  72. global _start

  73. _start:
  74.     ; 调用printf函数显示欢迎消息
  75.     push message
  76.     call printf
  77.     add esp, 4

  78.     ; 调用printf函数提示用户输入半径
  79.     push prompt
  80.     call printf
  81.     add esp, 4

  82.     ; 调用libPuhfessorP函数获取用户输入的半径
  83.     call libPuhfessorP
  84.     mov [radius], eax   ; 将半径存储到radius变量中

  85.     ; 调用printf函数显示接收到的半径
  86.     push dword [radius]
  87.     push received
  88.     call printf
  89.     add esp, 8

  90.     ; 计算圆的面积
  91.     mov eax, [radius]
  92.     imul eax, eax      ; 半径的平方
  93.     imul eax, 22       ; 倍增22
  94.     mov edx, 7         ; 除以7的结果存放在edx寄存器中
  95.     idiv edx
  96.     mov ebx, eax

  97.     ; 调用printf函数显示圆的面积
  98.     push ebx
  99.     push result
  100.     call printf
  101.     add esp, 8

  102.     ; 调用printf函数显示结束语
  103.     push goodbye
  104.     call printf
  105.     add esp, 4

  106.     ; 返回给主程序
  107.     mov eax, 1
  108.     xor ebx, ebx
  109.     int 0x80
复制代码


请注意,这只是一个示例框架,你需要根据你的实际需求进行修改和完善。另外,还需要确保你调用的外部库函数(libPuhfessorP)能够正确地获取用户输入的半径。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-4-10 07:15:06 | 显示全部楼层
FishC 发表于 2024-4-8 15:26
首先,你需要确保你使用的是汇编语言来编写该程序。你可以使用汇编器(如NASM)来将你的代码转换成可执行文 ...

不太对啊,人工智障的回答还差一点
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-4-10 07:15:21 | 显示全部楼层
wsjusen 发表于 2024-04-10 07:15
不太对啊,人工智障的回答还差一点


非常抱歉,可能我的回答没有完全理解你的问题。你似乎需要一个汇编程序来计算圆的面积,并使用外部库函数获取用户输入的半径。

下面是一个更加详细和清晰的示例代码,供你参考:

  1. section .data
  2.     prompt db 'Please enter the radius of a circle in whole number of meters: ', 0
  3.     result db 'The area of a circle with this radius is %d and 1/7 square meters.', 0
  4.     goodbye db 'The integer part of the area will be returned to the main program. Please enjoy your circles.', 0

  5. section .text
  6.     extern printf, scanf

  7. global main
  8. main:
  9.     ; 显示提示信息,要求用户输入半径
  10.     push prompt
  11.     call printf
  12.     add esp, 4

  13.     ; 获取用户输入的半径并存储在变量radius中
  14.     lea eax, [radius]
  15.     push eax
  16.     push intFormat
  17.     call scanf
  18.     add esp, 8

  19.     ; 计算圆的面积
  20.     fldpi                 ; 将派加载到FPU寄存器栈中
  21.     fld dword [radius]    ; 加载半径到FPU寄存器栈中
  22.     fmulp                 ; 乘法操作,得到圆的面积
  23.     fistp dword [area]    ; 将结果转换为整数并存储在area变量中

  24.     ; 显示计算得到的圆的面积
  25.     push dword [area]
  26.     push result
  27.     call printf
  28.     add esp, 8

  29.     ; 显示结束语
  30.     push goodbye
  31.     call printf
  32.     add esp, 4

  33.     ; 返回给主程序
  34.     xor eax, eax
  35.     ret

  36. section .bss
  37.     radius resd 1
  38.     area resd 1

  39. section .data
  40.     intFormat db '%d', 0

  41. section .text
  42. extern printf, scanf

  43. global main
  44. main:
  45.     ; 显示提示信息,要求用户输入半径
  46.     push prompt
  47.     call printf
  48.     add esp, 4

  49.     ; 获取用户输入的半径并存储在变量radius中
  50.     lea eax, [radius]
  51.     push eax
  52.     push intFormat
  53.     call scanf
  54.     add esp, 8

  55.     ; 计算圆的面积
  56.     fldpi                 ; 将派加载到FPU寄存器栈中
  57.     fld dword [radius]    ; 加载半径到FPU寄存器栈中
  58.     fmulp                 ; 乘法操作,得到圆的面积
  59.     fistp dword [area]    ; 将结果转换为整数并存储在area变量中

  60.     ; 显示计算得到的圆的面积
  61.     push dword [area]
  62.     push result
  63.     call printf
  64.     add esp, 8

  65.     ; 显示结束语
  66.     push goodbye
  67.     call printf
  68.     add esp, 4

  69.     ; 返回给主程序
  70.     xor eax, eax
  71.     ret

  72. section .bss
  73.     radius resd 1
  74.     area resd 1
复制代码


请注意,此代码使用了FPU(浮点处理单元)来进行浮点数的运算和转换,并且使用了C标准库函数`printf`和`scanf`来进行输入输出操作。确保你的汇编器和链接器支持这些操作,并且正确地链接外部库函数。另外,你可能需要根据你的编译环境和平台进行一些调整。

希望这次回答能够满足你的需求,如果还有任何问题,请随时提问!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 02:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表