diff --git a/os_worksheet_2/.gitignore b/os_worksheet_2/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/os_worksheet_2/Makefile b/os_worksheet_2/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..44c64a19b572c3b069c11796edf12ddcae016ad5 --- /dev/null +++ b/os_worksheet_2/Makefile @@ -0,0 +1,30 @@ +OBJECTS = loader.o kmain.o +CC = gcc +CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \ + -nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c +LDFLAGS = -T link.ld -melf_i386 +AS = nasm +ASFLAGS = -f elf + +all: kernel.elf + +kernel.elf: $(OBJECTS) + ld $(LDFLAGS) $(OBJECTS) -o kernel.elf + +os.iso: kernel.elf + cp kernel.elf iso/boot/kernel.elf + genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot \ + -boot-load-size 4 -A os -input-charset utf8 -quiet \ + -boot-info-table -o os.iso iso + +run: os.iso + bochs -f bochsrc.txt -q + +%.o: %.c + $(CC) $(CFLAGS) $< -o $@ + +%.o: %.s + $(AS) $(ASFLAGS) $< -o $@ + +clean: + rm -rf *.o kernel.elf os.iso diff --git a/os_worksheet_2/README.md b/os_worksheet_2/README.md new file mode 100644 index 0000000000000000000000000000000000000000..2b2742fd17bb22ac50f93722e32856146d2b045e --- /dev/null +++ b/os_worksheet_2/README.md @@ -0,0 +1,4 @@ +Worksheet 2 +Task 1 +- It's not possible to find 0xCAFEBABE in the logQ.txt because it doesn't use hex to store registers. +Instead, you can find EAX=CAFEBABE \ No newline at end of file diff --git a/os_worksheet_2/driver.o b/os_worksheet_2/driver.o new file mode 100644 index 0000000000000000000000000000000000000000..607579d934e1e2f639b3510e342c575c83249be5 Binary files /dev/null and b/os_worksheet_2/driver.o differ diff --git a/os_worksheet_2/iso/boot/grub/menu.lst b/os_worksheet_2/iso/boot/grub/menu.lst new file mode 100644 index 0000000000000000000000000000000000000000..9c6bcbe56095f4e520178625d4681017c167280e --- /dev/null +++ b/os_worksheet_2/iso/boot/grub/menu.lst @@ -0,0 +1,5 @@ +default=0 + timeout=0 + + title os + kernel /boot/kernel.elf \ No newline at end of file diff --git a/os_worksheet_2/iso/boot/grub/stage2_eltorito b/os_worksheet_2/iso/boot/grub/stage2_eltorito new file mode 100644 index 0000000000000000000000000000000000000000..9e1617cee78bb8f1ab6838837c9a1f36d729c9fd Binary files /dev/null and b/os_worksheet_2/iso/boot/grub/stage2_eltorito differ diff --git a/os_worksheet_2/iso/boot/kernel.elf b/os_worksheet_2/iso/boot/kernel.elf new file mode 100755 index 0000000000000000000000000000000000000000..38b2f1cc4bf6126c5cb654cb55afc075512ac08a Binary files /dev/null and b/os_worksheet_2/iso/boot/kernel.elf differ diff --git a/os_worksheet_2/kernel.elf b/os_worksheet_2/kernel.elf new file mode 100755 index 0000000000000000000000000000000000000000..f67c7dc3e9146d92415ef99645560e65edb2a768 Binary files /dev/null and b/os_worksheet_2/kernel.elf differ diff --git a/os_worksheet_2/kmain.o b/os_worksheet_2/kmain.o new file mode 100644 index 0000000000000000000000000000000000000000..c9a3eaae2cdd65f3f51040057cdc76bec79222c2 Binary files /dev/null and b/os_worksheet_2/kmain.o differ diff --git a/os_worksheet_2/link.ld b/os_worksheet_2/link.ld new file mode 100644 index 0000000000000000000000000000000000000000..9fc5fce56adea82a0b2eb70f05d69886b26d6409 --- /dev/null +++ b/os_worksheet_2/link.ld @@ -0,0 +1,26 @@ +ENTRY(loader) /* The name of the entry label */ + +SECTIONS { + . = 0x00100000; /* The code should be loaded at 1MB */ + + .text ALIGN(0x1000): /* Align at 4KB */ + { + *(.text) /* All text sections from all files */ + } + + .rodata ALIGN(0x1000): /* Align at 4KB */ + { + *(.rodata*) /* All read-only data sections from all files */ + } + + .data ALIGN(0x1000): /* Align at 4KB */ + { + *(.data) /* All data sections from all files */ + } + + .bss ALIGN(0x1000): /* Align at 4KB */ + { + *(COMMON) /* All COMMON sections from all files */ + *(.bss) /* All bss sections from all files */ + } +} \ No newline at end of file diff --git a/os_worksheet_2/loader.o b/os_worksheet_2/loader.o new file mode 100644 index 0000000000000000000000000000000000000000..6cad81ad77f029b9a8406cb4eb38939c113c4649 Binary files /dev/null and b/os_worksheet_2/loader.o differ diff --git a/os_worksheet_2/logQ.txt b/os_worksheet_2/logQ.txt new file mode 100644 index 0000000000000000000000000000000000000000..9bb53152d123bdf6d55f7aec63ec0819d8b840d7 Binary files /dev/null and b/os_worksheet_2/logQ.txt differ diff --git a/os_worksheet_2/os.iso b/os_worksheet_2/os.iso new file mode 100644 index 0000000000000000000000000000000000000000..3c3eac8a9def446e14b9c09234ae863b39c701d2 Binary files /dev/null and b/os_worksheet_2/os.iso differ diff --git a/os_worksheet_2/source/kmain.c b/os_worksheet_2/source/kmain.c new file mode 100644 index 0000000000000000000000000000000000000000..5dc53530fd029751eb87799920e33275e4c6139b --- /dev/null +++ b/os_worksheet_2/source/kmain.c @@ -0,0 +1,5 @@ + /* The C function */ + int sum_of_three(int arg1, int arg2, int arg3) + { + return arg1 + arg2 + arg3; + } \ No newline at end of file diff --git a/os_worksheet_2/source/loader.asm b/os_worksheet_2/source/loader.asm new file mode 100644 index 0000000000000000000000000000000000000000..5472cb145a9f7ae30ca15ee8c9a933f8393344d1 --- /dev/null +++ b/os_worksheet_2/source/loader.asm @@ -0,0 +1,29 @@ +global loader + +; Define constants +MAGIC_NUMBER equ 0x1BADB002 +FLAGS equ 0x0 +CHECKSUM equ -MAGIC_NUMBER ; Calculate the checksum +; (magic number + checksum + flags should equal 0) +KERNEL_STACK_SIZE equ 4096 +section .bss +align 4 +kernel_stack: +resb KERNEL_STACK_SIZE +section .text + align 4 ; The code must be 4-byte aligned + dd MAGIC_NUMBER ; Write the magic number to the machine + dd FLAGS ; The flags + dd CHECKSUM ; The checksum + extern sum_of_three + +loader: ; The loader label (defined as entry point in linker script) + mov eax, 0xCAFEBABE ; Place the number 0xCAFEBABE in the register eax + mov esp, kernel_stack + KERNEL_STACK_SIZE + push dword 3 + push dword 2 + push dword 1 + call sum_of_three + +.loop: + jmp .loop ; Loop forever \ No newline at end of file diff --git a/os_worksheet_2/source/loader.o b/os_worksheet_2/source/loader.o new file mode 100644 index 0000000000000000000000000000000000000000..77a0c8895f412835d6ed7210d4b37985b671e9e1 Binary files /dev/null and b/os_worksheet_2/source/loader.o differ diff --git a/src/asm_io.asm b/src/asm_io.asm new file mode 100644 index 0000000000000000000000000000000000000000..ad52628a8c8d8660a16e358d2e282ec49252d599 --- /dev/null +++ b/src/asm_io.asm @@ -0,0 +1,502 @@ +; +; file: asm_io.asm +; Assembly I/O routines +; To assemble for DJGPP +; nasm -f coff -d COFF_TYPE asm_io.asm +; To assemble for Borland C++ 5.x +; nasm -f obj -d OBJ_TYPE asm_io.asm +; To assemble for Microsoft Visual Studio +; nasm -f win32 -d COFF_TYPE asm_io.asm +; To assemble for Linux +; nasm -f elf -d ELF_TYPE asm_io.asm +; To assemble for Watcom +; nasm -f obj -d OBJ_TYPE -d WATCOM asm_io.asm +; IMPORTANT NOTES FOR WATCOM +; The Watcom compiler's C library does not use the +; standard C calling convention. For example, the +; putchar() function gets its argument from the +; the value of EAX, not the stack. + + +%define NL 10 +%define CF_MASK 00000001h +%define PF_MASK 00000004h +%define AF_MASK 00000010h +%define ZF_MASK 00000040h +%define SF_MASK 00000080h +%define DF_MASK 00000400h +%define OF_MASK 00000800h + + +; +; Linux C doesn't put underscores on labels +; +; %ifdef ELF_TYPE +%define _scanf scanf +%define _printf printf +%define _getchar getchar +%define _putchar putchar +; %endif + +; ; +; ; Watcom puts underscores at end of label +; ; +; %ifdef WATCOM +; %define _scanf scanf_ +; %define _printf printf +; %define _getchar getchar_ +; %define _putchar putchar_ +; %endif + +%ifdef OBJ_TYPE +segment .data public align=4 class=data use32 +%else +segment .data +%endif + +int_format db "%i", 0 +string_format db "%s", 0 +reg_format db "Register Dump # %d", NL + db "EAX = %.8X EBX = %.8X ECX = %.8X EDX = %.8X", NL + db "ESI = %.8X EDI = %.8X EBP = %.8X ESP = %.8X", NL + db "EIP = %.8X FLAGS = %.4X %s %s %s %s %s %s %s", NL + db 0 +carry_flag db "CF", 0 +zero_flag db "ZF", 0 +sign_flag db "SF", 0 +parity_flag db "PF", 0 +overflow_flag db "OF", 0 +dir_flag db "DF", 0 +aux_carry_flag db "AF", 0 +unset_flag db " ", 0 +mem_format1 db "Memory Dump # %d Address = %.8X", NL, 0 +mem_format2 db "%.8X ", 0 +mem_format3 db "%.2X ", 0 +stack_format db "Stack Dump # %d", NL + db "EBP = %.8X ESP = %.8X", NL, 0 +stack_line_format db "%+4d %.8X %.8X", NL, 0 +math_format1 db "Math Coprocessor Dump # %d Control Word = %.4X" + db " Status Word = %.4X", NL, 0 +valid_st_format db "ST%d: %.10g", NL, 0 +invalid_st_format db "ST%d: Invalid ST", NL, 0 +empty_st_format db "ST%d: Empty", NL, 0 + +; +; code is put in the _TEXT segment +; +%ifdef OBJ_TYPE +segment text public align=1 class=code use32 +%else +segment .text +%endif + global read_int, print_int, print_string, read_char + global print_char, print_nl, sub_dump_regs, sub_dump_mem + global sub_dump_math, sub_dump_stack + extern _scanf, _printf, _getchar, _putchar + +read_int: + enter 4,0 + pusha + pushf + + lea eax, [ebp-4] + push eax + push dword int_format + call _scanf + pop ecx + pop ecx + + popf + popa + mov eax, [ebp-4] + leave + ret + +print_int: + enter 0,0 + pusha + pushf + + push eax + push dword int_format + call _printf + pop ecx + pop ecx + + popf + popa + leave + ret + +print_string: + enter 0,0 + pusha + pushf + + push eax + push dword string_format + call _printf + pop ecx + pop ecx + + popf + popa + leave + ret + +read_char: + enter 4,0 + pusha + pushf + + call _getchar + mov [ebp-4], eax + + popf + popa + mov eax, [ebp-4] + leave + ret + +print_char: + enter 0,0 + pusha + pushf + +%ifndef WATCOM + push eax +%endif + call _putchar +%ifndef WATCOM + pop ecx +%endif + + popf + popa + leave + ret + + +print_nl: + enter 0,0 + pusha + pushf + +%ifdef WATCOM + mov eax, 10 ; WATCOM doesn't use the stack here +%else + push dword 10 ; 10 == ASCII code for \n +%endif + call _putchar +%ifndef WATCOM + pop ecx +%endif + popf + popa + leave + ret + + +sub_dump_regs: + enter 4,0 + pusha + pushf + mov eax, [esp] ; read FLAGS back off stack + mov [ebp-4], eax ; save flags + +; +; show which FLAGS are set +; + test eax, CF_MASK + jz cf_off + mov eax, carry_flag + jmp short push_cf +cf_off: + mov eax, unset_flag +push_cf: + push eax + + test dword [ebp-4], PF_MASK + jz pf_off + mov eax, parity_flag + jmp short push_pf +pf_off: + mov eax, unset_flag +push_pf: + push eax + + test dword [ebp-4], AF_MASK + jz af_off + mov eax, aux_carry_flag + jmp short push_af +af_off: + mov eax, unset_flag +push_af: + push eax + + test dword [ebp-4], ZF_MASK + jz zf_off + mov eax, zero_flag + jmp short push_zf +zf_off: + mov eax, unset_flag +push_zf: + push eax + + test dword [ebp-4], SF_MASK + jz sf_off + mov eax, sign_flag + jmp short push_sf +sf_off: + mov eax, unset_flag +push_sf: + push eax + + test dword [ebp-4], DF_MASK + jz df_off + mov eax, dir_flag + jmp short push_df +df_off: + mov eax, unset_flag +push_df: + push eax + + test dword [ebp-4], OF_MASK + jz of_off + mov eax, overflow_flag + jmp short push_of +of_off: + mov eax, unset_flag +push_of: + push eax + + push dword [ebp-4] ; FLAGS + mov eax, [ebp+4] + sub eax, 10 ; EIP on stack is 10 bytes ahead of orig + push eax ; EIP + lea eax, [ebp+12] + push eax ; original ESP + push dword [ebp] ; original EBP + push edi + push esi + push edx + push ecx + push ebx + push dword [ebp-8] ; original EAX + push dword [ebp+8] ; # of dump + push dword reg_format + call _printf + add esp, 76 + popf + popa + leave + ret 4 + +sub_dump_stack: + enter 0,0 + pusha + pushf + + lea eax, [ebp+20] + push eax ; original ESP + push dword [ebp] ; original EBP + push dword [ebp+8] ; # of dump + push dword stack_format + call _printf + add esp, 16 + + mov ebx, [ebp] ; ebx = original ebp + mov eax, [ebp+16] ; eax = # dwords above ebp + shl eax, 2 ; eax *= 4 + add ebx, eax ; ebx = & highest dword in stack to display + mov edx, [ebp+16] + mov ecx, edx + add ecx, [ebp+12] + inc ecx ; ecx = # of dwords to display + +stack_line_loop: + push edx + push ecx ; save ecx & edx + + push dword [ebx] ; value on stack + push ebx ; address of value on stack + mov eax, edx + sal eax, 2 ; eax = 4*edx + push eax ; offset from ebp + push dword stack_line_format + call _printf + add esp, 16 + + pop ecx + pop edx + + sub ebx, 4 + dec edx + loop stack_line_loop + + popf + popa + leave + ret 12 + + +sub_dump_mem: + enter 0,0 + pusha + pushf + + push dword [ebp+12] + push dword [ebp+16] + push dword mem_format1 + call _printf + add esp, 12 + mov esi, [ebp+12] ; address + and esi, 0FFFFFFF0h ; move to start of paragraph + mov ecx, [ebp+8] + inc ecx +mem_outer_loop: + push ecx + push esi + push dword mem_format2 + call _printf + add esp, 8 + + xor ebx, ebx +mem_hex_loop: + xor eax, eax + mov al, [esi + ebx] + push eax + push dword mem_format3 + call _printf + add esp, 8 + inc ebx + cmp ebx, 16 + jl mem_hex_loop + + mov eax, '"' + call print_char + xor ebx, ebx +mem_char_loop: + xor eax, eax + mov al, [esi+ebx] + cmp al, 32 + jl non_printable + cmp al, 126 + jg non_printable + jmp short mem_char_loop_continue +non_printable: + mov eax, '?' +mem_char_loop_continue: + call print_char + + inc ebx + cmp ebx, 16 + jl mem_char_loop + + mov eax, '"' + call print_char + call print_nl + + add esi, 16 + pop ecx + loop mem_outer_loop + + popf + popa + leave + ret 12 + +; function sub_dump_math +; prints out state of math coprocessor without modifying the coprocessor +; or regular processor state +; Parameters: +; dump number - dword at [ebp+8] +; Local variables: +; ebp-108 start of fsave buffer +; ebp-116 temp double +; Notes: This procedure uses the Pascal convention. +; fsave buffer structure: +; ebp-108 control word +; ebp-104 status word +; ebp-100 tag word +; ebp-80 ST0 +; ebp-70 ST1 +; ebp-60 ST2 ... +; ebp-10 ST7 +; +sub_dump_math: + enter 116,0 + pusha + pushf + + fsave [ebp-108] ; save coprocessor state to memory + mov eax, [ebp-104] ; status word + and eax, 0FFFFh + push eax + mov eax, [ebp-108] ; control word + and eax, 0FFFFh + push eax + push dword [ebp+8] + push dword math_format1 + call _printf + add esp, 16 +; +; rotate tag word so that tags in same order as numbers are +; in the stack +; + mov cx, [ebp-104] ; ax = status word + shr cx, 11 + and cx, 7 ; cl = physical state of number on stack top + mov bx, [ebp-100] ; bx = tag word + shl cl,1 ; cl *= 2 + ror bx, cl ; move top of stack tag to lowest bits + + mov edi, 0 ; edi = stack number of number + lea esi, [ebp-80] ; esi = address of ST0 + mov ecx, 8 ; ecx = loop counter +tag_loop: + push ecx + mov ax, 3 + and ax, bx ; ax = current tag + or ax, ax ; 00 -> valid number + je valid_st + cmp ax, 1 ; 01 -> zero + je zero_st + cmp ax, 2 ; 10 -> invalid number + je invalid_st + push edi ; 11 -> empty + push dword empty_st_format + call _printf + add esp, 8 + jmp short cont_tag_loop +zero_st: + fldz + jmp short print_real +valid_st: + fld tword [esi] +print_real: + fstp qword [ebp-116] + push dword [ebp-112] + push dword [ebp-116] + push edi + push dword valid_st_format + call _printf + add esp, 16 + jmp short cont_tag_loop +invalid_st: + push edi + push dword invalid_st_format + call _printf + add esp, 8 +cont_tag_loop: + ror bx, 2 ; mov next tag into lowest bits + inc edi + add esi, 10 ; mov to next number on stack + pop ecx + loop tag_loop + + frstor [ebp-108] ; restore coprocessor state + popf + popa + leave + ret 4 diff --git a/src/asm_io.inc b/src/asm_io.inc new file mode 100644 index 0000000000000000000000000000000000000000..9163be3955bb418e8b5d74d9004b38cc45f7cfa4 --- /dev/null +++ b/src/asm_io.inc @@ -0,0 +1,30 @@ + extern read_int, print_int, print_string + extern read_char, print_char, print_nl + extern sub_dump_regs, sub_dump_mem, sub_dump_math, sub_dump_stack + +%macro dump_regs 1 + push dword %1 + call sub_dump_regs +%endmacro + + +; +; usage: dump_mem label, start-address, # paragraphs +%macro dump_mem 3 + push dword %1 + push dword %2 + push dword %3 + call sub_dump_mem +%endmacro + +%macro dump_math 1 + push dword %1 + call sub_dump_math +%endmacro + +%macro dump_stack 3 + push dword %3 + push dword %2 + push dword %1 + call sub_dump_stack +%endmacro \ No newline at end of file diff --git a/src/asm_io.o b/src/asm_io.o new file mode 100644 index 0000000000000000000000000000000000000000..d2921f0c774600089db73d581638199c58d10228 Binary files /dev/null and b/src/asm_io.o differ diff --git a/src/driver.c b/src/driver.c new file mode 100644 index 0000000000000000000000000000000000000000..9e52b250583054d9bf7c9e47500ba08d584e45be --- /dev/null +++ b/src/driver.c @@ -0,0 +1,9 @@ +#include <stdio.h> + +int __attribute__((cdecl)) asm_main(void); + +int main() { + int ret_status; + ret_status = asm_main(); + return ret_status; +} \ No newline at end of file diff --git a/src/driver.o b/src/driver.o new file mode 100644 index 0000000000000000000000000000000000000000..d9c964d5c502ff05525b278dcc90cc8150982da0 Binary files /dev/null and b/src/driver.o differ diff --git a/src/task1 b/src/task1 new file mode 100755 index 0000000000000000000000000000000000000000..0e1baab6567574a518846afa48fb21dd393f2c34 Binary files /dev/null and b/src/task1 differ diff --git a/src/task1.asm b/src/task1.asm new file mode 100644 index 0000000000000000000000000000000000000000..b73a11737dc17e20a7549e33a4f92b705310c7f2 --- /dev/null +++ b/src/task1.asm @@ -0,0 +1,13 @@ +section .data + msg db "Hello from asm_main!", 0 + +section .text + extern printf + global asm_main + +asm_main: + push msg + call printf + add esp, 4 ; Clean up the stack + xor eax, eax ; Return 0 + ret \ No newline at end of file diff --git a/src/task1.o b/src/task1.o new file mode 100644 index 0000000000000000000000000000000000000000..bee39e3a6eea0278a5c4c0706ecbd02b356544e5 Binary files /dev/null and b/src/task1.o differ diff --git a/src/task2.asm b/src/task2.asm new file mode 100644 index 0000000000000000000000000000000000000000..23a3c9f9eb37a2b8eb35c3142be6f9bc8d0cd770 --- /dev/null +++ b/src/task2.asm @@ -0,0 +1,51 @@ +%include "asm_io.inc" + +section .data + prompt_name db "Enter your name: ", 0 + prompt_times db "Enter the number of times (51-99) to print the welcome message: ", 0 + invalid_input db "Invalid input! Please enter a number between 51 and 99.", 10, 0 + welcome_msg db "Welcome, %s!", 10, 0 + +section .bss + name resb 50 ; Reserve space for the name (50 bytes) + times resd 1 ; Reserve space for the number (4 bytes) + +section .text + extern asm_main ; Declared in driver.c + global asm_main + +asm_main: + ; Prompt for name + mov eax, prompt_name ; Load the address of the prompt string + call print_string ; Print the string + + mov eax, name ; Load the address of the name buffer + mov ebx, 50 ; Set max input length + call read_string ; Read user input into the name buffer + +ask_times: + ; Prompt for the number of times + mov eax, prompt_times ; Load the address of the prompt string + call print_string ; Print the string + + call read_int ; Read an integer from the user + mov [times], eax ; Store the input number in the `times` variable + + ; Validate the input (51 <= times <= 99) + mov eax, [times] ; Load the number into eax + cmp eax, 51 ; Check if less than 51 + jl invalid_input_msg ; Jump if invalid + cmp eax, 99 ; Check if greater than 99 + jg invalid_input_msg ; Jump if invalid + jmp print_messages ; Valid input, proceed + +invalid_input_msg: + mov eax, invalid_input ; Load the invalid input message + call print_string ; Print the string + jmp ask_times ; Re-prompt the user + +print_messages: + mov ecx, [times] ; Load the number of times into ecx (loop counter) + +print_loop: + mov eax, welcome_msg ; Lo diff --git a/src/test b/src/test new file mode 100755 index 0000000000000000000000000000000000000000..0f67137503f75efd5016ec14f8b0384f4c20e021 Binary files /dev/null and b/src/test differ diff --git a/src/test.asm b/src/test.asm new file mode 100644 index 0000000000000000000000000000000000000000..60e4c1146884df0cb6cf4615e3f34ef359b18044 --- /dev/null +++ b/src/test.asm @@ -0,0 +1,41 @@ +segment .data + prompt db "Enter your name: ", 0 + format db "%s", 0 + welcome_msg db "Welcome, %s!", 0 + newline db 10, 0 +segment .bss + name resb 50 ; Reserve space for the name (50 bytes) + +segment .text + extern printf + extern scanf + +global asm_main +asm_main: + ; Print the prompt + push prompt + call printf + add esp, 4 ; Clean up the stack + + ; Read the input + push name ; Address of the buffer to store the input + push format ; Format string for scanf + call scanf + add esp, 8 ; Clean up the stack (2 arguments) + + ; Print the welcome message + push name ; Push the name to print + push welcome_msg + call printf + add esp, 8 ; Clean up the stack (2 arguments) + + ; Print newline + push newline + call printf + add esp, 4 + + ; Exit the program + mov eax, 1 ; syscall: exit + xor ebx, ebx ; exit code 0 + ret + int 0x80 \ No newline at end of file diff --git a/src/test.o b/src/test.o new file mode 100644 index 0000000000000000000000000000000000000000..80cb795da7a45f9eea54d6014ee8e95195c3c1ee Binary files /dev/null and b/src/test.o differ diff --git a/src/test1 b/src/test1 new file mode 100755 index 0000000000000000000000000000000000000000..82dc7e1279aa15c6de4c0a5f546b1657e2d4d406 Binary files /dev/null and b/src/test1 differ diff --git a/src/test1.asm b/src/test1.asm new file mode 100644 index 0000000000000000000000000000000000000000..35f59598881c05d1760c8126093d8d385fa7f0cc --- /dev/null +++ b/src/test1.asm @@ -0,0 +1,43 @@ +segment .data + prompt db "Enter your name: ", 0 + format db "%s", 0 + welcome_msg db "Welcome, %s!", 0 + newline db 10, 0 + number dd 0 + count db 0 +segment .bss + name resb 50 ; Reserve space for the name (50 bytes) + +segment .text + extern printf + extern scanf + +global asm_main +asm_main: + ; Print the prompt + push prompt + call printf + add esp, 4 ; Clean up the stack + + ; Read the input + push name ; Address of the buffer to store the input + push format ; Format string for scanf + call scanf + add esp, 8 ; Clean up the stack (2 arguments) + + ; Print the welcome message + push name ; Push the name to print + push welcome_msg + call printf + add esp, 8 ; Clean up the stack (2 arguments) + + ; Print newline + push newline + call printf + add esp, 4 + + ; Exit the program + mov eax, 1 ; syscall: exit + xor ebx, ebx ; exit code 0 + ret + int 0x80 \ No newline at end of file diff --git a/src/test1.o b/src/test1.o new file mode 100644 index 0000000000000000000000000000000000000000..798af099283855e8416c322fece231be2ebb8f04 Binary files /dev/null and b/src/test1.o differ