Subtraction of decimal values ​​(assembly)

How to subtract decimal value in assembly . IA32 (linux)

1: mov edx, 1/2 sub ecx, ebx sub ecx, edx mov bal2, ecx 

I tried this, but it somehow skips the decimal point subtraction. And if I enter .5 , this will lead to an error.

error: junk `.5 'after expression

+4
source share
2 answers

As the light bulb correctly says in its answer, you cannot work with floating point values ​​when using general purpose registers ( eax , etc.). You can use the FPU as the light bulb suggests, but it is relatively tedious.

Alternatively, if your code should not run on older processors, you can use SSE instructions for this task, which is much simpler. Code similar to what you are showing might look like this:

 .data OneHalf dd 0.5 bal2 dd ? .code movss xmm0, OneHalf ; instead of mov edx, 1/2 subss xmm1, xmm2 ; instead of sub ecx, ebx subss xmm1, xmm0 ; instead of sub ecx, edx movss bal2, xmm1 ; instead of mov bal2, ecx 
+4
source

The code you provided is not able to subtract the "decimal value". I assume that under β€œdecimal value” you really have a floating point value, in which case you cannot use integer arithmetic tools like you did.

I highly recommend that you download the Intel IA-64 Architecture Software Developer's Guide ( Intel IA-64 Architecture Mnaul Software Developer ) and read the sections that explain how to use x87 floating point objects. In particular, see:

  • Volume 1, Section 5.2
  • Volume 1, Chapter 8
  • Volume 2A Section 3.2

As a warning, my experience with floating point objects is very minimal. However, I understand that this is a stack environment in which you can load (push) an item onto the stack and then work with it. The commands I would like to see are: FLD, FSUB, and FST (all of which are listed in Volume 2A).

An example is a short program that loads +1 and the constant pi on the floating point stack, then performs the pi-1 operation and returns the result.

 /* floating point subtraction */ .text .globl _main 

_main: pushq% rbp movq% rsp,% rbp

  fld1 /* load +1.0 */ fldpi /* load pi (3.14159...) */ fsubp /* 3.14159 - 1.0, pop result */ movq $0x0, %rax leave ret 
+2
source

All Articles