0

I want to write a program which converts angles to radians.

format ELF executable 3
entry start

segment readable executable

deg2rad:
    mov ax, [val] 
    mov bx, 180 ; rad = deg*pi/180
    mul [pi]    ;rad = deg*pi/180
    div ebx

start:
    mov [rad], ax  ; get rad from ax
    add [rad],'0'  ; to string
    mov ax,4       ; output
    mov bx,1       ; output
    mov cx,[rad]   ; output
    mov dx,10      ; output
    int 0x80       ; output
exit:
    mov eax,1
    xor ebx,ebx
    int 0x80


segment readable writeable

step dw 5
val dw 0.5
pi dw 3.14
rad dw ?

But after compiling and starting program I doesn't display the result. Why? I think error in output. How fix?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    Why are you using 16-bit registers? And who is supposed to initialise `AX` at the program entry-point? `sys_write` expects a pointer and radians are rarely integers – Margaret Bloom Nov 06 '18 at 16:21
  • Then how can I display the float number? – Hello World Nov 06 '18 at 16:55
  • 1
    @HelloWorld You have to manually convert the float to an integer. Alternatively, you can use a routine that does that for you, e.g. the one implemented by `printf` from the libc. That said, note that you are doing integer arithmetic on floating point numbers. I'm not sure how this is supposed to work. – fuz Nov 06 '18 at 16:56
  • 3
    *"I think error in output."* ... not really... except the exit service pretty much everything there is "wrong" in one way or another. Assembly doesn't work like you expect in many ways. Try to get some tutorial and examples, and start with simpler things... Also this may be worth of reading for you (although you are not that far yet, maybe you will get better idea why mixing floats with integer math does not work as expected): https://floating-point-gui.de/ – Ped7g Nov 06 '18 at 17:32
  • `[flatassembler]` should be a synonym of `[fasm]`, right? Vote at https://stackoverflow.com/tags/fasm/synonyms to merge them. (ping @Ped7g) – Peter Cordes Nov 07 '18 at 01:00
  • @PeterCordes I don't have required score on the tag to vote... sorry (but yes, it should be) – Ped7g Nov 07 '18 at 07:59
  • As already noted, you need to study some basic x86 assembly language, particularly in regard to handling numbers, number formats, and registers. Here's a related question regarding [handling floating point in x86](https://stackoverflow.com/questions/8804770/how-to-divide-floating-point-number-in-x86-assembly). – lurker Nov 07 '18 at 15:24
  • @PeterCordes I don't have enough `[fasm]` reputation to do so :-( – fuz Nov 08 '18 at 11:00

0 Answers0