12
\$\begingroup\$

As an exercise, I've created a simple solution for this challenge, in x86 Assembly Language. I'm running this with FASM on Windows. Here's my source code:

format PE console
entry start

include 'WIN32A.inc'

section '.text' code executable
start:
    push    char            ; Start at 'A'
    call    [printf]        ; Print the current letter 4 times
    call    [printf]
    call    [printf]
    call    [printf]
    inc     [char]          ; Increment the letter
    cmp     [char], 'Z'     ; Compare to 'Z'
    jle     start           ; char <= 'Z' --> goto start

section 'r.data' data readable writeable
    char    db  'A', 10, 0  ; Stores the current letter

section '.idata' data readable import
    library  msvcrt,   'msvcrt.dll'
    import   msvcrt, printf, 'printf'

When I compile this, I get an executable larger than I expected. Here's a hexdump:

https://pastebin.com/W5sUTvTe

I notice there's a lot of empty space between the code section and the data and library import sections, as well as a message saying "This program cannot be run in DOS mode" embedded in the code. How can I assemble my source code to a small file, suitable for Code Golf?

As a side note, suggestions for better ways to print to stdout without importing msvcrt and calling printf are welcome.

\$\endgroup\$
  • \$\begingroup\$ @iBug I'm sorry to hear that. Could you please suggest a more suitable place for me to ask? \$\endgroup\$ – vasilescur Nov 21 '17 at 13:22
  • 12
    \$\begingroup\$ @iBug Tips questions asking for golfing help in specific cases are most definitely not off-topic here. \$\endgroup\$ – AdmBorkBork Nov 21 '17 at 13:22
  • 12
    \$\begingroup\$ Relevant meta \$\endgroup\$ – AdmBorkBork Nov 21 '17 at 13:25
  • 1
    \$\begingroup\$ It has to be: start: push char Lb: call [printf] call [printf] call [printf] call [printf] inc [char] cmp [char], 'Z' jle Lb because if not, could be consume the stack; one has to see if each call to printf one has to add the instruction that adjust esp \$\endgroup\$ – RosLuP Nov 21 '17 at 14:55
  • 1
    \$\begingroup\$ instead of printf, you can WriteFile(stdout), needing no imports other than kernel32 (which is present by default, you just need to determine the address) \$\endgroup\$ – peter ferrie Nov 24 '17 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.