0

I'm trying to translate assembly code, which gets system date and time into inline-assembly code in C.

The assembly code I'm trying to translate looks something like this (just a part of the actual code)

    MOV     AH, 2AH
    INT     21H

    MOV     BL, DL
    MOV     BH, 0
    PUSH    BX
    CALL    P_PRINT_NUM             ; function to print number

The C code I made looks like

uint8_t test;
asm("movb $0x2A, %%ah;"
    "int $0x21;"
    "movb %0, %%dl;"
     : "=r"(test));

which compiles just fine using GCC. However, when I run the program I get a segmentation fault error. I tried some things like change the constraint or the type of variable but without success. I'm pretty new to inline-assembly. Any help will be greatly appreciated. Thanks.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
nmck
  • 160
  • 1
  • 10
  • 6
    What are you trying to run this on? `INT 21H` is how you call into MS-DOS. If you're not running on MS-DOS, it won't work. – Roger Lipscombe Apr 05 '16 at 08:53
  • While `INT` is a valid x86 instruction, I assume this won't work, due to `INT 21H` being typically used for DOS calls, and not available on modern Windows anymore. Also, this looks like 16-bit assembly. – Leandros Apr 05 '16 at 08:58
  • I don't understand. The original code is for x86 16-bit DOS. How do you get a segmentation fault under DOS??? – Laszlo Valko Apr 05 '16 at 08:59
  • 2
    @LaszloValko it's probably because he's _not_ running it on MSDOS. – Jabberwocky Apr 05 '16 at 09:00
  • @RogerLipscombe Original code was made to run on MS-DOS, but the C code is supposet to run on Linux. Didn't realize that I had to change the interupt. Thanks. – nmck Apr 05 '16 at 09:01
  • @nmck it won't run on Linux. As soon as you use inline assembly, your program is no more _pure_ C. You can use inline assembly only for a specific platform. You inline assembly code is specific to MSDOS, so no chance for it to run on Linux, Windows or anything else different from MSDOS. – Jabberwocky Apr 05 '16 at 09:05
  • If you want to get the system date and time in C, see http://stackoverflow.com/questions/5141960/get-the-current-time-in-c – Roger Lipscombe Apr 05 '16 at 09:30
  • I needed a solution to get a date and time without use of library. Meanwhile I found a solution with `syscall(SYS_time);`. Anyway, thank you for the help. – nmck Apr 05 '16 at 12:04

0 Answers0