打印
[STM32F4]

如何通过一次除法,同时获得余数和商?

[复制链接]
1397|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
pjzmj2012|  楼主 | 2018-5-19 11:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 pjzmj2012 于 2018-5-20 23:28 编辑

如何通过一次除法,同时获得余数和商?
stm32系列芯片、IAR环境


#include <stdlib.h>
div_t  x;
x=div(A,B);
C = x.quot;
D = x.rem;
这个函数倒是可以做到,但是这样我就没办法四舍五入了。

沙发
Lewisnx| | 2018-5-19 11:44 | 只看该作者
一次除法?除不完怎么办?
期待实现方法

使用特权

评论回复
板凳
CCompton| | 2018-5-19 11:59 | 只看该作者
网上搜的,不知道对不对
#include <stdio.h>


// dividend, divisor, quotient and remainder should be replaced with variables instead of constant number
#define GET_DIV_AND_MOD(dividend, divisor, quotient, remainder)        \
__asm{        \
__asm push eax        \
__asm push edx        \
__asm mov eax, dword ptr[dividend]        \
__asm cdq        \
__asm idiv dword ptr[divisor]        \
__asm mov dword ptr[quotient], eax        \
__asm mov dword ptr[remainder], edx        \
__asm pop edx        \
__asm pop eax        \
}


void main(void)
{
int a = 9, b = 4;

int quo=0, rem=0;

//GET_DIV_AND_MOD(a, b, quo, rem);

quo = a / b;

GET_DIV_AND_MOD(a, b, quo, rem);

         printf("The quo is: %d, and the rem is: %d\n", quo, rem);
}

使用特权

评论回复
地板
mmuuss586| | 2018-5-19 12:29 | 只看该作者
自己写个子程序好了;

使用特权

评论回复
5
mcu5i51| | 2018-5-20 15:52 | 只看该作者
不是有成品函数吗

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

35

主题

130

帖子

1

粉丝