Hi i trying to make a loop in Nasm. I have the loop working but want to change the string when it loops and cant seem to get this to work.
I have this working:
main:
mov ebx,0 ; set ebx to 0
myloop: ;
inc ebx ;
mov [msg], 2Ah ; Immediate move * to msg
mov eax, 2Ah ;
push ecx ; save ecx as printf uses it
push OFFSET msg ; parameter used by printf
call printf ; print string msg
add esp, 4 ; remove pointer to msg
pop ecx ; restore ecx
cmp ebx,[len] ;
jl myloop ;
ret
.DATA
msg db "Hello, World+!", 0Ah, 0 ; 0A cariage return
; 0 end of string
star BYTE 2Ah ; 2A (hex) = Ascii *
len equ $ - (msg +1) ; length of string msg
end
and it outputs *ello, World! the right number of times but i want it to output this
*ello, World!
**llo, World!
***lo, World!
****o, World!
and so on.
Can anyone help me please.
I must also note that it is linked to a C library to use printf for out put.
I won't comment it
SECTION .data
msg db "Hello, World+!", 0Ah, 0
len equ $ - msg - 2
SECTION .text
main:
xor ebx, ebx
mov esi, msg
push esi
call printf
add esp, 4
.DoStar:
mov byte [esi + ebx], 2Ah
push esi
call printf
add esp, 4
inc ebx
cmp ebx, len
jne .DoStar
ret
Related
I would like to make a copy of a string and store the copy in another variable. I want to do it the most basic way, cause I have just started learning Assembly.
I have something like this:
section .data
mystring db "string goes here", 10 ;string
mystringlen equ $- mystring ;length
helper dw 0 ;variable to help me in a loop
Now, I thought of a loop that will take each byte of the string and assign it to new string.
I have this (I know it only changes initial string, but doesn't work either):
loopex:
mov [mystring+helper], byte 'n' ;change byte of string to 'n'
add helper, 1 ;helper+=1
cmp helper,mystringlen ;compare helper with lenght of string
jl loopex
;when loop is done
mov eax, 4
mov ecx, mystring ; print changed string
mov edx, mystringlen ; number of bytes to write
;after printing
int 0x80 ; perform system call
mov eax, 1 ; sys_exit system call
mov ebx, 0 ;exit status 0
int 0x80
So I would need something like:
old_string = 'old_string'
new_string = ''
counter=0
while(counter!=len(old_string)):
new_string[counter] = old_string[counter]
counter+=1
print(new_string)
The code after help I received:
section .data
mystring db "This is the string we are looking for", 10
mystringlen equ $- mystring
section .bss
new_string: resb mystringlen
section .text
global _start
_start:
mov ecx, 0
mov esi, mystring
mov edi, new_string
loopex: ; do {
mov byte al, [esi]
mov byte [edi], al
inc esi
inc edi
inc ecx
cmp ecx, mystringlen
jl loopex ; }while(++ecx < mystringlen)
;when loop is done
mov eax, 4 ; __NR_write (asm/unistd_32.h)
mov ebx, 1 ; fd = STDOUT_FILENO
mov ecx, new_string ; bytes to write
mov edx, mystringlen ; number of bytes to write
int 0x80 ; perform system call: write(1, new_string, mystringlen)
;after printing
mov eax, 1 ; sys_exit system call number
mov ebx, 0 ; exit status 0
int 0x80
I'm trying to check the length of a sting given in the program argument as part of a larger program. I need to place the value of the string length in a variable called n, which I've left uninitialized in BSS. I've tried a couple different methods of doing this, including the one Im trying right now which was given in another answer here. However when I try to print or use the result of this little algorithm, it always returns 7. I believe I'm getting the address of what I want and not the result but I cant figure what to change. Heres my code:
%include "asm_io.inc"
SECTION .data
fmt1: db "%d",10,0
argErrMsg: db "Needs 2 args",10,0
argOkMsg : db "Arguments ok",10,0
doneMsg: db "Program finished, now exiting",10,0
strErrMsg: db "String must be between 1 and 20 charaters",10,0
strOkMsg: db "String length ok",10,0
SECTION .bss
X: resd 20
i: resd 1
n: resd 1
k: resd 1
SECTION .text
global asm_main
extern printf
extern strlen
asm_main:
enter 0,0
pusha
CHECK_ARGS:
cmp dword [ebp+8],2
jne ERROR_ARGS
je OK_ARGS
ERROR_ARGS:
push dword argErrMsg
call printf
add esp,8
jmp EXIT
OK_ARGS:
push dword argOkMsg
call printf
add esp,8
jmp CHECK_STRING
CHECK_STRING:
mov eax, dword[ebp+16]
push eax ;This is the code I tried using from another answer
mov ecx,0
dec eax
count:
inc ecx
inc eax
cmp byte[eax],0
jnz count
dec ecx
ret
pop eax
mov [n],ecx ;Tried prining it here to see the result
push dword [n]
push dword fmt1
call printf
add esp,8
cmp byte [n],1
jl ERROR_STRING
cmp byte [n],20
jg ERROR_STRING
jmp OK_STRING ;The program always gets to here since [n] = 7?
ERROR_STRING:
push dword strErrMsg
call printf
add esp,8
jmp EXIT
OK_STRING:
push dword strOkMsg
call printf
add esp,8
jmp EXIT
EXIT:
push dword doneMsg
call printf
popa
leave
ret
To get the length of argv[1]:
mov edi,[ebp+12] ;edi = argv = &argv[0]
mov edi,[edi+4] ;edi = argv[1]
mov ecx,0ffffh ;scan for 0
xor eax,eax
repne scasb
sub ecx,0ffffh ;ecx = -(length + 1)
not ecx ;ecx = length
main should return a 0 in eax:
popa
xor eax,eax
leave
ret
Hi every one I am trying to do a tower of Hanoi in assembly x86 but I am trying to use arrays. So this code gets a number from user as a parameter in Linux, then error checks a bunch of stuff. So now I just want to make the algorithm which use the three arrays i made (start, end, temp) and output them step by step. If someone can help it would be greatly appreciated. `
%include "asm_io.inc"
segment .data ; where all the predefined variables are stored
aofr: db "Argument out of Range", 0 ; define aofr as a String "Argument out of Range"
ia: db "Incorrect Argument", 0 ; define ia as a String "Incorrect Argument"
tma: db "Too many Arguments", 0 ; define tma as a String "Too many Arguments"
hantowNumber dd 0 ; define hantowNumber this is what the size of the tower will be stored in
start: dd 0,0,0,0,0,0,0,0,9 ; this array is where all the rings start at
end: dd 0,0,0,0,0,0,0,0,9 ; this array is where all the rings end up at
temp: dd 0,0,0,0,0,0,0,0,9 ; this array is used to help move the rings
test: dd 0,0,0,0,0,0,0,0,9
; The next couple lines define the strings to show the pegs and what they look like
towerName: db " Tower 1 Tower 2 Tower 3 ", 10, 0
lastLineRow: db "XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX ", 10, 0
buffer: db " ", 0
fmt:db "%d",10,0
segment .bss ; where all the input variables are stored
segment .text
global asm_main ; run the main function
extern printf
asm_main:
enter 0,0 ; setup routine
pusha
mov edx, dword 0 ; set edx to zero this is where the hantowNumber is saved for now
mov ecx, dword[ebp+8] ; ecx has how many arguments are given
mov eax, dword[ebp+12] ; save the first argument in eax
add eax, 4 ; move the pointer to the main argument
mov ebx, dword[eax] ; save the number into ebx
push ebx ; reserve ebx
push ecx ; reserve ecx
cmp ecx, dword 2 ; compare if there are more the one argument given
jg TmA ; if more then one argument is given then jump Too many Argument (TmA)
mov ecx, 0 ; ecx = 0
movzx eax, byte[ebx+ecx] ; eax is the first character number from the inputed number
sub eax, 48 ; subtract 48 to get the actual number/letter/symbol
cmp eax, 10 ; check if eax is less then 10
jg IA ; if eax is greater then 10 then it is a letter or symbol
string_To_int: ; change String to int procedure
add edx, eax ; put the number in edx
inc ecx ; increase counter (ecx)
movzx eax, byte[ebx+ecx] ; move the next number in eax
cmp eax, 0 ; if eax = 0 then there are no more numbers
mov [hantowNumber], edx ; change hantowNumber to what ever is in edx
je rangeCheck ; go to rangeCheck to check if between 2-8
sub eax, 48 ; subtract 48 to get the actual number/letter/symbol
cmp eax, 10 ; check if eax is less then 10
jg IA ; if eax is greater then 10 then it is a letter or symbol
imul edx, 10 ; multiply edx by 10 so the next number can be added to the end
jmp string_To_int ; jump back to string_To_int if not done
rangeCheck: ; check the range of the number
cmp edx, dword 2 ; compare edx with 2
jl AofR ; if hantowNumber (edx) < 2 then go to Argument out of Range (AofR)
cmp edx, dword 8 ; compare edx with 8
jg AofR ; if hantowNumber (edx) > 8 then go to Argument out of Range (AofR)
mov ecx, [hantowNumber] ; move the number enterd by user in ecx
mov esi, 28 ; esi == 28 for stack pointer counter
setStart: ; set the first array the starting peg
mov [start+esi], ecx ; put ecx into the array
sub esi, 4 ; take one away from stack pointer conter
dec ecx ; take one away from ecx so the next number can go in to the array
cmp ecx, 0 ; compare ecx with 0
jne setStart ; if ecx != 0 then go to setStart loop
; This is the section where the algoritham should go for tower of hanoi
mov ecx, [hantowNumber]
towerAlgorithm:
cmp ecx, 0
jg Exit ; jump to Exit at the end of the program
dec ecx
IA:
mov eax, ia ; put the string in eax
push eax ; reserve eax
call print_string ; output the string that is in eax
call print_nl ; print a new line after the output
pop eax ; put eax back to normal
add esp, 4 ; takes 4 from stack
jmp Exit ; jump to Exit at the end of the program
AofR:
mov eax, aofr ; put the string in eax
push eax ; reserve eax
call print_string ; output the string that is in eax
call print_nl ; print a new line after the output
pop eax ; put eax back to normal
add esp, 4 ; takes 4 from stack
jmp Exit ; jump to Exit at the end of the program
TmA:
mov eax, tma ; put the string in eax
push eax ; reserve eax
call print_string ; output the string that is in eax
call print_nl ; print a new line after the output
pop eax ; put eax back to normal
add esp, 4 ; takes 4 from stack
jmp Exit ; jump to Exit at the end of the program
Exit: ; ends the program when it jumps to here
add esp, 9 ; takes 8 from stack
popa
mov eax, 0 ; return back to C
leave
ret
haha I'm doing the exact same assignment and stuck on the algorithm however though when running your code it seems to identify "too many arguments" even though only one argument is provided, consider this algorithm when dealing with arguments(don't forget ./ is considered the "first argument" since it is the zeroth argument provided):
enter 0,0
pusha
; address of 1st argument is on stack at address ebp+12
; address of 2nd arg = address of 1st arg + 4
mov eax, dword [ebp+12] ;eax = address of 1st arg
add eax, 4 ;eax = address of 2nd arg
mov ebx, dword [eax] ;ebx = 2nd arg, it is pointer to string
mov eax, 0 ;clear the register
mov al, [ebx] ;it moves only 1 byte
sub eax, '0' ;now eax contains the numeric value of the firstcharacter of string
I am writing a program to get an integer from the user, and then print out all the numbers from 0 up to the number. My code gets the input fine, but when printing it out, it prints continuously in what seems to be an endless loop. Here is my code:
SECTION .data ; Constant variable declaration
len EQU 32 ; Constant length
msg db "Enter a number: ", 0 ; Input message
msglen EQU $-msg ; Input message length
SECTION .bss ; Uninitialised data declaration
other resd len ; Output counter that is incremented
data resd len ; Input data buffer
SECTION .text ; Main program initialiser
GLOBAL _start ; Linker entry point declaration
_start: ; Entry point
nop ; This keeps the debugger happy :)
Msg: ; This section prints out the message
mov eax, 4 ; }
mov ebx, 1 ; }
mov ecx, msg ; } System_write call
mov edx, msglen ; }
int 80h ; }
input: ; This section gets the integer from the user
mov eax, 3 ; }
mov ebx, 0 ; }
mov ecx, data ; } System_read call
mov edx, len ; }
int 80h ; }
ASCIIAdj:
mov ebp, 48 ; This line sets the counter to '0' ASCII
setup: ; This section adjusts the counter
mov [other], ebp ; Increment counter
loop: ; This section loops, printing out from zero to the number given
mov eax, 4 ; }
mov ebx, 1 ; }
mov ecx, other ; } System_write call
mov edx, len ; }
int 80h ; }
mov eax, 1 ; Move 1 to eax
add ebp, eax ; Add eax to ebp (essentially increment ebp)
mov eax, other ; move other to eax
mov ebx, data ; move data to ebx
cmp eax, ebx ; compare them
jne setup ; If they are not the same, go back to the setup to increment other
exit: ; Exits the program
mov eax, 1 ; }
mov ebx, 0 ; } System_exit call
int 80h ; }
Why does it loop continuously? I have incremented the counter, and compared the input and the counter, so why doesn't it break?
Thanks in advance
EDIT:
Expected Output:
Enter a number: 6
0123456
General Semantics of the program:
Display "Enter a number: "
Read in an integer less than 32 bytes in size.
Set a counter variable to the ASCII value of zero
Loop:
Display the character, adding 1 to it, and checking to see if it is equal to the value inputted.
If it is equal, goto the exit section and exit
Else loop.
This is digging waaaay back into the deep dark recesses of my memory, but I think you want
mov eax, [other] ; move other to eax
mov ebx, [data] ; move data to ebx
Note the brackets, which are missing in your code. You are loading the addresses of other and data into eax and ebx, not the values contained there.
I keep getting a segmentation fault error when running my code. Everything has compiled well, but I can't seem to get it to do what I want. The program is to ask the user to enter 3 integers, then ask the user what they think the average of the numbers would be, take that into account and then come back with whether or not the user guessed correctly
segment .data
;
; Output strings
;
prompt1 db "Enter a positive integer: ", 0
prompt2 db "Enter a second positive integer: ", 0
prompt3 db "Enter a third positive integer: ", 0
prompt4 db "Enter a guess of their average: ", 0
outmsg1 db "You entered ", 0
outmsg2 db " and ", 0
outmsg3 db " and ", 0
outmsg4 db "You guessed that the average is ", 0
outmsg5 db "You did you guess correctly? (0 = no, 1 = yes)", 0
avermsg db "The average of the numbers is ", 0
segment .bss
input1 resd 1
input2 resd 1
input3 resd 1
input4 resd 1
guess resd 1
segment .text
Global main
main:
enter 0,0 ; setup routine
pusha
mov eax, prompt1 ; print out prompt1
call print_string
call read_int ; read integer
mov [input1], eax ; store integer into input1
mov eax, prompt2 ; print out prompt2
call print_string
call read_int ; read integer
mov [input2], eax ; store integer into input2
mov eax, prompt3 ; print out prompt3
call print_string
call read_int ; read integer
mov [input3], eax ; store integer into input3
mov eax, prompt4 ; print out prompt4
call print_string
call read_int ; read integer
mov [guess], eax
mov eax, [input1] ; eax = dword at input1
add eax, [input2] ; eax += dword at input2
add eax, [input3] ; eax += dword at input3
mov ebx, 3
div ebx ; divides the sum by 3
mov ecx, eax ; freeing up eax, puts quotient into ecx
dump_regs 1 ; print out register values
; next print out results
mov eax, outmsg1
call print_string ; print out first message
mov eax, [input1]
call print_int
mov eax, outmsg2
call print_string ; print out second message
mov eax, [input2]
call print_int
mov eax, outmsg3
call print_string ; print out thrid message
mov eax, [input3]
call print_int
mov eax, outmsg4
call print_string ; print out fourth message
mov eax, [input4]
call print_int
xor ebx, ebx
cmp ecx, [guess]
sete bl
neg ebx
mov edx, ebx
and ecx, edx
not ebx
and ebx, [guess]
or edx, ebx
mov eax, outmsg5
call print_string
mov ecx, eax
call print_int
mov eax, [avermsg]
call print_string ; print out final message
mov ecx, edx
call print_int ; print out average of ebx
call print_nl ; print new line
popa
mov eax, 0 ; return back to C
leave
ret
It is not easy to pinpoint the problem without knowing the compiler you use.
Segmentation fault hapens in protected mode, when you try to access a segment you have no permissions to access.
You declare 3 diferent segments here. You have to make sure your ds register is initialized to your .data segment, before calling print_string.
It also seems problematic that after read_int you save the data to the input1 variable which seems to be in a different segment than that you used for printing the message, but you do not change ds.
I'm not familiar how exactly your compiler handles these segments, so please give a link to its documentation.
div ebx
the problem seems to be here. You have to zero out edx since is is the high order word of the divisor, so probably you're getting something like a division overflow exception.
If this isn't the case probably the problem is in some of your I/O routines