0

I' learning assembly and trying to process arguments. I can't figure out how to print nth character from arguments.

Here is code segment from my program:

CODE SEGMENT
ASSUME CS:CODE, DS:DATA, SS:ZAS
START:
    ; print nth character from arguments
    mov dl, 81h+n ; replace n with number
    mov ah, 2
    int 21h

    ; end program
    mov ah, 4ch
    int 21h
CODE ENDS
     END START

Not sure if I'm using incorrect syntax, or if I'm missing something important.

Edit: I found the solution to my problem.

mov dl, byte ptr ds:[82h]
mov ah, 2
int 21h

When I run exe file like this: "filename.exe arg" then at address 81h is byte ' '. At the address 82h is the first byte 'a'. Therefore my argument starts at address 82h relative to the ds. Also I have to read the argument before I load data segment [mov ax, SEG DATA; mov ds, ax].

  • 1
    Are you trying to load from memory at offset `81h`? That syntax will mov an immediate into `dl`, as you can see with a debugger. Looks like MASM / TASM syntax so you'd need `mov dl, cs:81h + 5` or whatever or `byte ptr [81h + 5]`; not sure if you need to use the code segment in an EXE or if DS will also be set appropriately. (In a .com they're all set the same) – Peter Cordes Feb 22 '22 at 09:14
  • 2
    @PeterCordes: In a flat .COM program file's process indeed all segment registers are initially the same. An MZ .EXE program file specifies its own `ss` and `cs` displacements which can result in different segments than the PSP. `es` and `ds` always start out pointing to the PSP regardless of executable format. – ecm Feb 22 '22 at 10:53
  • 1
    @ecm: Thanks, then `mov dl, byte ptr [81h + 5]` *before* doing `mov ax, @data` / `mov ds, ax` should always work. I assume there's an existing duplicate for this somewhere, perhaps just [Confusing brackets in MASM32](https://stackoverflow.com/q/25129743) for the immediate vs. memory syntax problem. – Peter Cordes Feb 22 '22 at 22:04
  • How do I access that value, when I want to use data variable? If I have variable in data segment: `psp_ptr db 82h` I want to use something like this: `mov dl, byte ptr ds:[psp_ptr]` – Martin Šváb Feb 23 '22 at 16:13

0 Answers0