2

I'm learning assembly for 80x86 this semester. A typical asm file I write looks something like

.model tiny
.486
.data
#initializations
.code
.startup
#actual code
.exit
end

I was expecting a similar format when I created a .s file for a simple hello world. But I don't see any of the segments with their proper names and it's all very different. I compile using g++ -S -O0 hello.c

Why is the assembly for c so different than the assembly they make us write in class? Is the assembly I'm learning used by a different programming language? If I want to get the assembly version (that I'm used to) of hello world from some higher-level code, how do I do that?

Sarthak
  • 43
  • 7
  • 3
    g++ and gcc belong to gnu toolchain and they use `as` or gnu assembler. Your code looks like Tasm / Masm family of assemblers, which are incompatible. – Aki Suihkonen Mar 21 '21 at 07:34
  • 1
    Does it have a `.text` segment? I think is GAS, it is called `.text` and not `.code` – mediocrevegetable1 Mar 21 '21 at 07:39
  • .text .globl _main .def _main; .scl 2; .type 32; .endef It does say .text here but doesn't seem to correlate with .code – Sarthak Mar 21 '21 at 07:45
  • @AkiSuihkonen my code is for masm611 assembler. If I wanted assembly for masm family, how do I get it? Do I use something other than g++ or gcc? – Sarthak Mar 21 '21 at 07:48
  • Well MASM is for Microsoft I think, so if you used MSVC I think you would get MASM output. You could also do `-masm=intel` when generating assembly with GCC but it wouldn't be exactly like MASM. – mediocrevegetable1 Mar 21 '21 at 07:52
  • Are you still using MS-DOS? – Weather Vane Mar 21 '21 at 07:55
  • @mediocrevegetable1 I've tried ```-masm=intel``` and it still doesn't look familiar. As for MSVC, I'll give it a shot – Sarthak Mar 21 '21 at 07:58
  • `.model tiny` Is your class about 16-bit x86 assembly? – dxiv Mar 21 '21 at 07:58
  • @WeatherVane I do use dosbox to compile and run my assembly code – Sarthak Mar 21 '21 at 07:58
  • @dxiv I can use model small also. I don't think that's relevant. – Sarthak Mar 21 '21 at 08:08
  • 1
    @WeatherVane In fairness, 16-bit x86 *can* teach some useful basics about assembly at large, *And* it may be more SO-proof in a way, though I didn't think at it from this angle before ;-) – dxiv Mar 21 '21 at 08:09
  • 1
    @dxiv I agree: learning the basics in a sand box is not unreasonable. People also learn to code processors that don't exist at all (MARIE). But I am sure that is not the reason: perhaps more to do with a culture that throws nothing away if it is of any use. – Weather Vane Mar 21 '21 at 08:10
  • @Sarthak Those memory models date back to 16-bit decades ago. To see anything remotely comparable nowadays, you'd need to find a C/C++ compiler capable of generating 16-bit machine code. – dxiv Mar 21 '21 at 08:11
  • Maybe [this post](https://stackoverflow.com/questions/3987138/compiling-as-16-bit-for-ms-dos-with-visual-studio) could help regarding that. – mediocrevegetable1 Mar 21 '21 at 08:14
  • Your `g++` isn't compiling for 16-bit DOS, so it's certainly not going to use `.model tiny`, even if GAS had an equivalent option. Related Q&As about GCC's asm output for Linux: [What is the meaning of each line of the assembly output of a C hello world?](https://stackoverflow.com/q/5325326) – Peter Cordes Mar 21 '21 at 08:15
  • Also related: [Hello, world in assembly language with Linux system calls?](https://stackoverflow.com/q/61519222) and/or [What is the explanation of this x86 Hello World using 32-bit int 0x80 Linux system calls from \_start?](https://stackoverflow.com/q/45052162) for what hand-written asm for x86 (32 and 64-bit) GNU/Linux looks like. Although from your `_main`, you're probably on Windows or MacOS. – Peter Cordes Mar 21 '21 at 08:28
  • @dxiv I'm only learning this because it is part of my course ;-; I would prefer learning a more directly useful version but alas, it is not in my hands – Sarthak Mar 21 '21 at 09:26
  • @WeatherVane perhaps – Sarthak Mar 21 '21 at 09:27
  • @dxiv know of any compiler capable of generating 16-bit machine code. Even code for a model small/medium/large/huge will do, as long as I can understand the rest of the code – Sarthak Mar 21 '21 at 09:29
  • @mediocrevegetable1 yeah ig that is what I'm looking for. I'll see if it actually works – Sarthak Mar 21 '21 at 09:34
  • `bcc` (Bruce's C Compiler) https://www.ibiblio.org/pub/micro/pc-stuff/freedos/files/distributions/1.2/repos/pkg-html/bcc.html targets 16-bit real mode in a small or tiny code model. I think it targets a different assembler than MASM, but the actual instructions should be useful. Like maybe as86 or GAS, not sure which. There's also ia-16 gcc (different from just using `gcc -m16`.) – Peter Cordes Mar 21 '21 at 13:59

1 Answers1

1

The code does not match your command line. That is neither C (file name) nor C++ code (command line). That is assembly language.

Assembly language varies by tool (masm, tasm, nasm, gas, etc), and is not expected to be compatible nor standard in any way. Not talking about just intel vs at&t, all of the code, and this applies to all targets not just x86, easily seen with ARM and others.

You should try to use the assembler not a C nor C++ compiler as that creates yet another assembly language even though gcc for example will pass the assembly language on to gas it can pre-process it through the C preprocessor creating yet another programming language that is incompatible with the gnu assembler it is fed to.

x86 is the last if ever assembly language/instruction set you want to learn, if you are going to learn it then starting with the 8086/88 is IMO the preferred way, much more understandable despite the nuances. Since this appears to be a class you are stuck with this ISA and cannot chose a better first instruction set. (first, second, third...)

Very much within the x86 world, but also for any other target, expect that the language is incompatible between tools and if it happens to work or mostly work that is a bonus. Likewise there is no reason to assume that any tool will have a "masm compatible" or other mode, simply stating intel vs at&t is only a fraction of the language problem and is in no way expected to make the code port between tools.

Re-write the code for the assembly language used for the assembler is the bottom line.

old_timer
  • 69,149
  • 8
  • 89
  • 168