2.masm32-mul/div/xchg(16进制转成10进制)

本文介绍了一个使用MASM汇编语言编写的程序,该程序实现了计算特定数值的阶乘,并通过自定义函数将计算结果转换为字符串形式进行输出。文章详细展示了如何利用MUL和DIV指令进行乘法和除法运算,以及如何运用XCHG指令交换寄存器内容。
        .386
        .model flat,stdcall
        option casemap:none


include windows.inc

include masm32.inc
include kernel32.inc
include user32.inc

includelib masm32.lib
includelib kernel32.lib
includelib user32.lib


        .data?
CharOut db 100 dup(?)

        .code

OutEdxEax PROTO :DWORD    

OutEdxEax proc lpString     ;定义OutEdxEax子函数          
        mov edi,lpString    ;指向存放结果的地址
        mov esi,lpString
        mov eax,123H        ;测试0x100000123的10进制大小
        mov edx,1
        mov ecx,10          ;转换成十进制
        .while eax!=0 || edx!=0
            push eax        ;eax压栈
            mov eax,edx     ;eax=edx
            xor edx,edx     ;edx=0
            div ecx         ;/10,eax存商,余数存edx
            mov ebx,eax     ;ebx存商
            pop eax         ;恢复eax
            div ecx         ;/10,eax存商,余数存edx
            add dl,'0'      ;dl增加'0',edx变成字符串
            mov [edi],dl    ;存放结果
            inc edi
            mov edx,ebx     ;商存入edx
        .endw

        mov BYTE ptr [edi],0;字符串以0为结尾
        dec edi

        .while edi>esi      ;结果前变后,后变前!
            mov al,[esi]
            xchg al,[edi]
            mov [esi],al
            inc esi
            dec edi
        .endw
        ret
OutEdxEax   endp

start:          ;程序开始
    xor eax,eax ;存放结果的寄存器EDX:EAX=1
    xor edx,edx
    inc eax
    mov ecx,20  ;计算20的阶乘。不能大于20,否则会溢出,结果将不再正确!
    .while ecx>0
        .if edx==0  ;结果在EAX中
            mul ecx
        .else       ;结果在EDX:EAX中
            push edx
            mul ecx
            pop ebx
            push eax
            push edx
            mov eax,ecx
            mul ebx
            pop edx
            add edx,eax
            pop eax
        .endif
        dec ecx
    .endw

    .if edx==0          ;结果在EAX中
        invoke dw2a,eax,addr CharOut
    .else               ;结果在EDX:EAX中
        invoke OutEdxEax,addr CharOut   ;用我们自己的程序转换!
    .endif

    invoke StdOut,addr CharOut  ;控制台输出结果
    invoke ExitProcess,NULL     ;结束程序
    end start

结果:

F:\MASM>ml /c /coff 3.asm
Microsoft (R) Macro Assembler Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: 3.asm

***********
ASCII build
***********


F:\MASM>link /subsystem:console 3.obj
Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.


F:\MASM>3.exe
4294967587
即0x100000123 = 4294967587


MUL:

操作数大小

源操作数 1

源操作数 2

目标操作数

字节

AL

r/m8

AX

AX

r/m16

DX:AX

双字

EAX

r/m32

EDX:EAX

如果参数是 r8/m8,   把AL做乘数, 结果放在AX
如果参数是 r16/m16, 把AX做乘数, 结果放在EAX
如果参数是 r32/m32, 把EAX做乘数, 结果放在EDX:EAX

结果存储到寄存器 AX、寄存器对 DX:AX 或 EDX:EAX(取决于操作数大小),乘积的高位分别保存在寄存器 AH、DX 或 EDX。如果乘积的高位均为 0,则清除 CF 与 OF 标志;否则将它们设置为 1。


DIV:

将 AX 寄存器、DX:AX 寄存器对或 EDX:EAX 寄存器对中的值(被除数)除以(无符号)源操作数(除数),结果存储到 AX (AH:AL)、DX:AX 或 EDX:EAX 寄存器。源操作数可以是通用寄存器或内存位置。此指令的操作取决于操作数大小,具体如下表所示:

操作数大小

被除数

 

除数

 

 

余数

商的最大值

字/字节

AX

r/m8

AL

AH

255

双字/字

DX:AX

r/m16

AX

DX

65,535

四字/双字

EDX:EAX

r/m32

EAX

EDX

232 - 1

如果参数是r8/m8,把AX做被除数;商->AL,余数->AH
如果参数是r16/m16,把DX:AX做被除数;商->AX,余数->DX
如果参数是r32/m32,把EDX:EAX做被除数;商->EAX,余数->EDX


XCHG:

操作码

指令

说明

90+rw

XCHG AX,16

交换 r16 与 AX

90+rw

XCHG r16, X

交换 AX 与 r16

90+rd

XCHG EAX, r32

交换 r32 与 EAX

90+rd

XCHG r32, EAX

交换 EAX 与 r32

86 /r

XCHG r/m8, r8

交换 r8(字节寄存器)与 r/m8 中的字节

86 /r

XCHG r8, r/m8

交换 r/m8 中的字节与 r8(字节寄存器)

87 /r

XCHG r/m16, r16

交换 r16 与 r/m16 中的字

87 /r

XCHG r16, r/m16

交换 r/m16 中的字与 r16

87 /r

XCHG r/m32, r32

交换 r32 与 r/m32 中的双字

87 /r

XCHG r32, r/m32

交换 r/m32 中的双字与 r32

交换目标(第一个)操作数与源(第二个)操作数的内容。操作数可以是两个通用寄存器,也可以是寄存器与内存位置













linux安装python依赖:[root@VM-16-5-centos noon]# pip3.6 install sqlalchemy WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3.6 install --user` instead. Collecting sqlalchemy Downloading http://mirrors.tencentyun.com/pypi/packages/5e/50/f63ff7811a8d3367a2c7fae6095f08da20e64e09762e4da1bf05706aefb1/SQLAlchemy-1.4.54-cp36-cp36m-manylinux1_x86_64.manylinux2010_x86_64.manylinux_2_12_x86_64.manylinux_2_5_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6MB) 100% |████████████████████████████████| 1.6MB 1.8MB/s Collecting greenlet!=0.4.17; python_version >= "3" and (platform_machine == "aarch64" or (platform_machine == "ppc64le" or (platform_machine == "x86_64" or (platform_machine == "amd64" or (platform_machine == "AMD64" or (platform_machine == "win32" or platform_machine == "WIN32")))))) (from sqlalchemy) Downloading http://mirrors.tencentyun.com/pypi/packages/1e/1e/632e55a04d732c8184201238d911207682b119c35cecbb9a573a6c566731/greenlet-2.0.2.tar.gz (164kB) 100% |████████████████████████████████| 174kB 920kB/s Collecting importlib-metadata; python_version < "3.8" (from sqlalchemy) Downloading http://mirrors.tencentyun.com/pypi/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl Collecting zipp>=0.5 (from importlib-metadata; python_version < "3.8"->sqlalchemy) Downloading http://mirrors.tencentyun.com/pypi/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl Requirement already satisfied: typing-extensions>=3.6.4; python_version < "3.8" in /usr/local/lib/python3.6/site-packages (from importlib-metadata; python_version < "3.8"->sqlalchemy) Installing collected packages: greenlet, zipp, importlib-metadata, sqlalchemy Running setup.py install for greenlet ... error Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-xwpa9djj/greenlet/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-5rl6bxpe-record/install-record.txt --single-version-externally-managed --compile: running install running build running build_py creating build creating build/lib.linux-x86_64-3.6 creating build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/__init__.py -> build/lib.linux-x86_64-3.6/greenlet creating build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/__init__.py -> build/lib.linux-x86_64-3.6/greenlet/platform creating build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_generator.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_stack_saved.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_contextvars.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_greenlet.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_cpp.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_gc.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_extension_interface.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_version.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_throw.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_greenlet_trash.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_weakref.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_tracing.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/__init__.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/leakcheck.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_generator_nested.py -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/test_leaks.py -> build/lib.linux-x86_64-3.6/greenlet/tests running egg_info writing src/greenlet.egg-info/PKG-INFO writing dependency_links to src/greenlet.egg-info/dependency_links.txt writing requirements to src/greenlet.egg-info/requires.txt writing top-level names to src/greenlet.egg-info/top_level.txt reading manifest file 'src/greenlet.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no previously-included files found matching 'benchmarks/*.json' no previously-included directories found matching 'docs/_build' warning: no files found matching '*.py' under directory 'appveyor' warning: no previously-included files matching '*.pyc' found anywhere in distribution warning: no previously-included files matching '*.pyd' found anywhere in distribution warning: no previously-included files matching '*.so' found anywhere in distribution warning: no previously-included files matching '.coverage' found anywhere in distribution writing manifest file 'src/greenlet.egg-info/SOURCES.txt' copying src/greenlet/greenlet.cpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet.h -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_allocator.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_compiler_compat.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_cpython_compat.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_exceptions.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_greenlet.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_internal.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_refs.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_slp_switch.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_thread_state.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_thread_state_dict_cleanup.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/greenlet_thread_support.hpp -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/slp_platformselect.h -> build/lib.linux-x86_64-3.6/greenlet copying src/greenlet/platform/setup_switch_x64_masm.cmd -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_aarch64_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_alpha_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_amd64_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_arm32_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_arm32_ios.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_arm64_masm.asm -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_arm64_masm.obj -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_arm64_msvc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_csky_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_m68k_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_mips_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc64_aix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc64_linux.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc_aix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc_linux.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc_macosx.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_ppc_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_riscv_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_s390_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_sparc_sun_gcc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x32_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x64_masm.asm -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x64_masm.obj -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x64_msvc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x86_msvc.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/platform/switch_x86_unix.h -> build/lib.linux-x86_64-3.6/greenlet/platform copying src/greenlet/tests/_test_extension.c -> build/lib.linux-x86_64-3.6/greenlet/tests copying src/greenlet/tests/_test_extension_cpp.cpp -> build/lib.linux-x86_64-3.6/greenlet/tests running build_ext building 'greenlet._greenlet' extension creating build/temp.linux-x86_64-3.6 creating build/temp.linux-x86_64-3.6/src creating build/temp.linux-x86_64-3.6/src/greenlet gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python3.6m -c src/greenlet/greenlet.cpp -o build/temp.linux-x86_64-3.6/src/greenlet/greenlet.o gcc: error trying to exec 'cc1plus': execvp: 没有那个文件或目录 error: command 'gcc' failed with exit status 1 ---------------------------------------- Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-xwpa9djj/greenlet/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-5rl6bxpe-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-xwpa9djj/greenlet/ [root@VM-16-5-centos noon]# python3.6 getSalerInfo3.py Traceback (most recent call last): File "getSalerInfo3.py", line 13, in <module> from sqlalchemy import exc, create_engine, text ModuleNotFoundError: No module named 'sqlalchemy' [root@VM-16-5-centos noon]#报错
最新发布
06-19
<think>我们面对的问题是:在CentOS系统上使用pip3.6安装SQLAlchemy时,依赖包greenlet编译失败,出现gcc或cc1plus错误。编译失败通常是由于系统缺少必要的开发工具或库文件。greenlet是一个需要编译的PythonC扩展模块,因此需要系统具备编译环境和必要的依赖。解决步骤一般包括:1.确保系统已安装开发工具(如gcc,g++,make等)和Python开发头文件。2.安装greenlet可能依赖的系统库(如果有的话)。3.检查错误日志,确定具体缺失的内容。4.根据错误信息安装缺失的组件。针对CentOS系统,我们可以按照以下步骤操作:###步骤1:安装开发工具和Python开发包在CentOS上,我们可以通过yum安装必要的开发工具和Python3开发包。```bashsudoyumgroupinstall"DevelopmentTools"sudoyuminstallpython36-devel#注意:根据你的Python版本,包名可能略有不同,如python3-devel,python36u-devel等```###步骤2:安装其他可能的依赖有时还需要安装一些额外的库,例如:```bashsudoyuminstallepel-release#如果还没有安装epel源sudoyuminstallopenssl-develbzip2-devellibffi-devel```###步骤3:尝试重新安装然后再次尝试安装SQLAlchemy(它会安装greenlet)或者直接安装greenlet:```bashpip3.6installSQLAlchemy```或者单独安装greenlet:```bashpip3.6installgreenlet```###步骤4:如果仍然失败,查看错误日志如果上述步骤后仍然失败,请仔细阅读错误日志。错误日志通常会指出具体缺少什么。比如,如果错误提到`Python.h`文件找不到,那说明Python开发包没有正确安装(即步骤1的python36-devel)。###步骤5:考虑使用wheel如果系统环境确实难以编译,可以考虑使用预编译的wheel。但greenlet的wheel可能不适用于所有平台。我们可以尝试:```bashpip3.6install--only-binary:all:greenlet```或者```bashpip3.6install--only-binary:all:SQLAlchemy```这样pip会尝试下载预编译的二进制包,避免编译过程。###其他注意事项-确保pip版本足够新:`pip3.6install--upgradepip`-如果系统中有多个Python版本,确保pip3.6对应的Python版本正确。如果以上步骤仍然不能解决问题,请提供具体的错误信息,以便进一步排查。###相关问题1.在CentOS上安装Python包时遇到编译错误,通常需要安装哪些基础包?2.如何解决PythonC扩展模块编译失败的问题?3.在缺少编译环境的服务器上如何安装需要编译的Python包?</think>### 解决CentOS中pip3.6安装SQLAlchemy时greenlet编译失败问题 在CentOS上安装SQLAlchemy时遇到greenlet编译错误,通常是由于缺少编译依赖或开发工具导致。以下是系统性的解决方案: #### 1. 安装基础编译工具 ```bash sudo yum groupinstall "Development Tools" sudo yum install gcc-c++ python36-devel ``` 这将安装GCC编译器套件、C++支持及Python 3.6开发头文件[^1]。 #### 2. 解决cc1plus错误 若出现`cc1plus: fatal error: Python.h: No such file or directory`: ```bash sudo yum install python36-devel # 确认已安装 export CFLAGS="-I/usr/include/python3.6m" # 手动指定头文件路径 ``` #### 3. 安装依赖库 ```bash sudo yum install openssl-devel bzip2-devel libffi-devel ``` #### 4. 使用预编译二进制(推荐) ```bash pip3.6 install --only-binary :all: SQLAlchemy # 或单独安装greenlet二进制 pip3.6 install --only-binary greenlet greenlet ``` #### 5. 升级工具链 ```bash sudo yum install centos-release-scl sudo yum install devtoolset-9 # 使用新版GCC scl enable devtoolset-9 bash # 激活新工具链 pip3.6 install SQLAlchemy ``` #### 6. 验证安装 ```bash python3.6 -c "from greenlet import greenlet; print('Success!')" ``` #### 常见错误处理: - **C++编译器缺失**:`sudo yum install gcc-c++` - **内存不足**:添加swap分区或使用`-j1`参数: ```bash pip3.6 install greenlet --global-option="-j1" ``` - **权限问题**:使用`--user`参数或虚拟环境 > 提示:完整日志可通过`pip3.6 install --verbose greenlet 2>&1 | tee build.log`获取,便于精确排查[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值