I am writing a lot to help clarify what I need, but my actual question is very s
ID: 3766646 • Letter: I
Question
I am writing a lot to help clarify what I need, but my actual question is very simple.
I need a portion of NASM assembly code please (using I/O asm_io.asm routines). Nothing too complicated, just very simple, beginner code (like the Hello World stuff used in someone's first course). I already have my assembly code, makefile, cdecl.h, driver.c etc etc. All I am missing is the few lines of code to modify it that will do what is specifically described below.
For example: make argv, then argv goodbye banana.
The loop would display the 2nd line argument (goodbye), go through the 3rd line argument (banana) one letter at a time counting the number of letters. If the number of letters in the 2nd string is greater than 4, then an error message would show. ie: argv goodbye banana would display:
goodbye
banana
more than 4
If the number of letters is equal to or less than 4, argv goodbye banana would display
goodbye
bana
Again, I do not need anything complicated, just a few basic lines using simple language to perform these loops using asm_io.asm routines (ie: mov ebx, dword [ebp+12] or call print_nl etc etc, this is the type of code I am missing in my .asm). I am new to this and appreciate help.
Can you please give a brief outline of each line so I understand and can write my own final code, thanks. (ie: mov [str1+1] ; means this and that will happen) Thank you!
Explanation / Answer
I hope, the below assembly code does what you are looking for.
First let me write some functions for doing basic tasks that we are going to perform in a separate file called func.asm
Func.asm
;------------------------------------------
; int slen(String input)
; String length calculation function
slen:
push ebx ; move the address of input string into EBX
mov ebx, eax ; move the address in EBX into EAX as well (Both now point to the same segment in memory)
nextchar:
cmp byte [eax], 0 ; compare the byte pointed to by EAX at this address against zero (Zero is an end of string delimiter)
jz finished ; jump (if the zero flagged has been set) to the point in the code labeled 'finished'
inc eax ; increment the address in EAX by one byte (if the zero flagged has NOT been set)
jmp nextchar ; jump to the point in the code labeled 'nextchar'
finished:
sub eax, ebx ; subtract the address in EBX from the address in EAX
; remember both registers started pointing to the same address (see line 15)
; but EAX has been incremented one byte for each character in the message string
; when you subtract one memory address from another of the same type
; the result is number of segments between them - in this case the number of bytes
pop ebx
ret
;------------------------------------------
; void sprint(String input)
; String printing function
sprint:
push edx ; Save all current register values to the stack
push ecx
push ebx
push eax
call slen ; calculate the length of the string
mov edx, eax ; move number of bytes to write – length of input string
pop eax ; fetch the input string
mov ecx, eax ; move the memory address of input string into ecx
mov ebx, 1 ; write to STDOUT file
mov eax, 4 ; invoke SYS_WRITE (kernel opcode 4)
int 80h
pop ebx
pop ecx
pop edx
ret
;------------------------------------------
; void exit()
; Exit program and restore resources
quit:
mov ebx, 0
mov eax, 1
int 80h
ret
This is the main file with the assembly instructions.
Include the file func.asm with the basic function definitions
Main.asm
;---------------------------------------------------------
%include 'func.asm'
SECTION .data
errmsg db 'more than 4', 0Ah, 0h
SECTION .text
global _start
_start:
pop ecx ; first value on the stack is the number of arguments
pop eax ; next value on the stack will be the name of the program
pop eax ; This will be the first argument
call sprint ; This will print the string on the screen
pop eax ; fetch the second argument from the stack
call sprint
call slen ; call the string length calculation function
mov edx, eax ; store the value i.e. length in ecx
cmp edx, 4 ; compare the length with value 4
jle end ; jump to end if less than or equal to 4
mov eax, errmsg ; if string length is not less than 4, display the errmsg
call sprint
end:
call quit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.