0

Thank you for taking the time to look at my question and all help is appreciated. I will keep this question simple, so here it is:

; Program Author: David Mark Serrano
; Program Name: Battle_Chess
; Date Created: July 12, 2023
; Version: 1.0
; Purpose: To render a textual chessboard on the Linux Terminal for a game of chess against an AI opponent

; Built using file "makefile":
;
; Battle_Chess: Board_Render.o Battle_Chess.o
;       ld -m elf_i386 -o Battle_Chess Battle_Chess.o Board_Render.o
;
; Board_Render.o: Board_Render.asm
;       nasm -f elf32 -g -o Board_Render.o Board_Render.asm
;
; Battle_Chess.o: Battle_Chess.asm
;       nasm -f elf32 -g -o Battle_Chess.o Battle_Chess.asm

 section .data ; Section for initialized data

; Buffer for 'GWINSZ' procedure
winsize_struct db 32

GWINSZ:
mov eax, 54 ; Specify `ioctl` syscall in 32-bit x86 assembly
mov ebx, 1 ; Stdin stored in ebx
mov ecx, 0x5413 ; 'ioctl' code for getting Terminal size
mov edx, winsize_struct ; Address of `winsize_struct` buffer is stored in `edx`
int 0x80 ; Syscall 128 / Vector Interrupt 128 (Kernel Services Dispatcher/Syscall Dispatcher)
ret ; Return from procedure

section .text
        global _start

_start:

        call GWINSZ ; Call 'GWINSZ' procedure defined above

        mov eax, 4 ; Specify 'sys_write' syscall in 32-bit x86 assembly language
        mov ebx, 1 ; STDOUT file descriptor stored in register 'ebx'
        mov ecx, winsize_struct ; winsize_struct buffer address stored in register 'ecx'
        mov edx, 32 ; Length of winsize_struct buffer in bytes
        int 0x80 ; Vector Interrupt 128 (Kernel Services/Syscall Dispatcher)

 ; This group of instructions (Line 97 - Line 97) will terminate the program and return code '0' to Linux to signal proper execution
        mov eax, 0x1 ; Specify sys_exit syscall
        xor ebx, ebx ; Clear `ebx` register so that it returns exit code 0
        int 0x80 ; Specify Vector Interrupt 128 (Kernel Services/Syscall Dispatcher)

This is the code that I wrote on my 2011 MacBook Air running Ubuntu 20.04.5 LTS (I know it's ancient). I am simply learning to write 32-bit x86 assembly using NASM (Intel syntax) and I'm working on my first project. My question is simple: Why can't the buffer 'winsize_struct' be printed? I want to retrieve the size of the Terminal and store that size in 'winsize_struct', and then print it out to the screen using the standard 'sys_write' syscall. I'm doing this because part of my project requires ensuring the terminal is opened up to a minimum size, and if it isn't then I will terminate the project prematurely with an error message informing the end-user to increase the Terminal to the minimum dimensions of $LINES and $COLUMNS. I am new to x86 assembly, please be nice and as helpful as you can because I have already tried many things to no avail.

Thank you for your help in advance.

I have already tried various things, but to no avail.

1.) I experimented with the 'GWINSZ' procedure, I changed the instruction 'mov edx, winsize_struct' to 'lea edx, [winsize_struct]'. This did not work and nothing is printed to the screen. 2.) I experimented with different methods for calling 'ioctl' in the 'GWINSZ' procedure. I settled for value 54 to be stored in register 'eax' since this is the syscall number for 'ioctl', I settled and 0x5413 as the TIOCGWINSZ code stored in register 'ecx'. 3.) I experimented with writing the 'winsize_struct' buffer in the 'section .bss' part of the program. There was no effect at all. 4.) I experimented with the value of register 'ebx' in the 'GWINSZ' procedure. I sometimes tried 0 or 1, but nothing seemed to be work.

I still do not know why there seems to be nothing stored in the buffer despite the apparent success in the syscall! How can I get the values $LINES and $COLUMNS to be printed?

  • 2
    The SysWrite system call takes a string to print, not integer values or sturctures to print. You would have to print out individual elements of the structure yourself and you would also have to convert numbers to strings. An alternative is to link against the C library and call something like `printf` which would simplify printing data from a structure. – Michael Petch Aug 27 '23 at 04:08
  • 1
    Well, that does make sense! I was experimenting with the instructions in 'GWINSZ' and settled back to 'lea ecx, [winsize_struct]' and I confirm that something does print with 'sys_write' syscall, but the characters printed aren't meaningfully displayed as text. So this does answer my question. Thank you and I appreciate the post very much. – HerrWeishaupt Aug 27 '23 at 04:18
  • 1
    What is being printed out is the binary version of the data (some of it is unprintable characters) in the structure. So if you were to pipe the output of your program through `hexdump` you could see a hex representation of the structure. – Michael Petch Aug 27 '23 at 04:57
  • 1
    [How do I print an integer in Assembly Level Programming without printf from the c library? (itoa, integer to decimal ASCII string)](https://stackoverflow.com/a/46301894) – Peter Cordes Aug 27 '23 at 05:38

0 Answers0