Find the length of a null terminated string (Assembly language) - winapi

I am trying to write a procedure to find the length of a null-terminated string and store the length in eax. The start address of the string is stored in edx before calling the procedure.
Here's my code, I don't know what's wrong:
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
myString BYTE "Hello programmers", 0
strLength PROC
mov eax , 0
mov ebx , '/0'
mov esi , 0
Condition:
movzx ecx , myString[esi]
cmp ebx , ecx
je ExitLoop
inc esi
inc eax
jmp Condition
ExitLoop:
ret
StrLength ENDP
main PROC
lea edx, myString
call strLength
INVOKE ExitProcess,0
main ENDP
END main
Here's the error message I'm getting:
Line 14 is : Condition
Line 21 is : ExitLoop:
Line 17 is : je ExitLoop
Line 20 is : jmp Condition
Line 29 is : call strLength
Line 38 is : INVOKE ExitProcess,0
and line 70 doesn't exist

Related

80x86 assembly code not returning from function and displaying output

The goal of the program is to calculate a GCD of two numbers using a recursive function defined by this pseudo code
unsigned int gcd ( unsigned int a, unsigned int b ) {
if ( a > b )
return gcd ( a - b, b ) ;
if ( a < b )
return gcd ( a, b - a ) ;
return a ;
}
Here is the (undoubtedly poor) assembly code I'm having issues with.
.586
.MODEL FLAT
INCLUDE io.h
.STACK 4096
.DATA
a DWORD ?
b DWORD ?
prompt1 BYTE "a: ", 0
prompt2 BYTE "b:", 0
string BYTE 20 DUP (?)
resultLbl BYTE "gcd:", 0
result BYTE 11 DUP (?), 0
.CODE
_MainProc PROC
input prompt1, string, 20
atod string
mov a, eax
input prompt2, string, 20
atod string
mov b, eax
push b
push a
call _gcd
add esp, 8
dtoa result, eax
output resultLbl, result
mov eax, 0
ret
_MainProc ENDP
_gcd PROC
push ebp
mov ebp, esp
push ebx
push eax
mov eax, [ebp+8]
mov ebx, [ebp+12]
cmp eax, ebx
jg loop1
cmp eax, ebx
jl loop2
pop ebx
pop ebp
ret
loop1:
push ebx
sub eax, ebx
push eax
call _gcd
loop2:
sub ebx, eax
push ebx
push eax
output did2, result
call _gcd
_gcd ENDP
END
By creating some outputs in the loops to display a message when they occur I can tell that the program is calculating the GCD correctly however as soon as both values are equal and "ret" is hit inside of _gcd the program terminates. What do I need to change so the GCD value is returned and displayed correctly?

Unable to display my output as required . I'm trying to program a decimal to binary converter in x86 assembly language in Visual Studio

This
is how my output should look like.
Here is my code:
.586
.MODEL FLAT
;Exit Process PROTO NEAR32 stdcall,dwExitCode:DWORD
INCLUDE io.h
cr EQU 0dh
Lf EQU 0ah
.STACK 4096
.DATA
prompt1 BYTE "Enter the decimal number: ", 0
asciiIn DW 4 DUP (?)
msgLabel BYTE "Result",00h; message Label
msgText BYTE "Your output" ;
BYTE "Decimal: "
asciiDecimal DW 4 DUP (?),10
BYTE "Binary: "
asciiBinary DW 4 DUP(?), 10
BYTE "Hex: "
asciiHex DW 4 DUP (?), 10
decimal WORD 0
STR1 DW 4 DUP (?)
STR2 DW 4 DUP (?)
HEX WORD 0
.CODE
_MainProc PROC
xor eax, eax ; clear eax
xor ebx, ebx ; clear ebx
xor ecx, ecx ; clear ecx
lp : input prompt1, asciiIn, 10 ; prompt user to enter number
atow asciiIn ; convert from ascii to word and store in eax
LEA ESI,STR1
; MOV EAX,NO
MOV BH,00
MOV BL,2
L1:DIV BL
ADD AH,'0'
MOV BYTE PTR[ESI],AH
MOV AH,00
INC ESI
INC BH
CMP AL,00
JNE L1
MOV CL,BH
LEA ESI,STR1
LEA EDI,STR2
MOV CH,00
ADD ESI,ECX
DEC ESI
L2:MOV AH,BYTE PTR[ESI]
MOV BYTE PTR[EDI],AH
DEC ESI
INC EDI
LOOP L2
done :
; output messages
output msgLabel,msgText
wtoa asciiDecimal, asciiIn
output asciiBinary,STR2
wtoa asciiHex, HEX
mov eax, 0
ret
_MainProc ENDP
END

fatal error LNK1120: 1 unresolved externals for assembly language using VS2010

Comment #
Using Programming Exercise 6 in Chapter 4 as a starting point,
write a program that generates the first 47 values in the Fibonacci
series, stores them in an array of doublewords, and writes the
doubleword array to a disk file.
#
INCLUDE c:\Irvine\Irvine32.inc
FIB_COUNT = 47 ; number of values to generate
.data
fileHandle DWORD ?
filename BYTE "fibonacci.bin",0
array DWORD FIB_COUNT DUP(?)
.code
main2sub PROC
; Generate the array of values
mov esi, OFFSET array
mov ecx, LENGTHOF array
call generate_fibonacci
; Create the file, call CreateOutputFile
mov edx,OFFSET filename
call CreateOutputFile
mov fileHandle, eax
; Write the array to the file, call WriteToFile
mov eax, fileHandle
mov edx, OFFSET array
mov ecx, FIB_COUNT * 4
call WriteToFile
; Close the file, call CloseFile
mov eax, fileHandle
call CloseFile
exit
main2sub ENDP
;------------------------------------------------------------
generate_fibonacci PROC USES eax ebx ecx edx
;
; Generates fibonacci values and stores in an array.
; Receives: ESI points to the array,
; ECX = count
; Returns: nothing
;------------------------------------------------------------
mov ebx, 1
mov ecx, 0
L1:
add ebx, ecx
mov [esi], eax
call WriteDec
call Crlf
inc esi
xchg ebx, eax
loop L1 ;end of looping
ret
generate_fibonacci ENDP
END main2sub
It is my first semester learning about Assembly Language. Im not sure what to do with this error message "fatal error LNK1120: 1 unresolved externals". Can anyone help me out?
The linker couldn't find the library-files Irvine32.lib, Kernel32.Lib and/or User32.Lib. They are in the same folder as Irvine32.inc
The easiest way is to inform ML about those libraries with a MASM-directive. Insert following three lines right behind the INCLUDE c:\Irvine\Irvine32.inc:
INCLUDELIB c:\Irvine\Irvine32.lib
INCLUDELIB c:\Irvine\Kernel32.lib
INCLUDELIB c:\Irvine\User32.lib

First macro assembler program, can't figure our unhandled exception

This is my first assembler program in masm32. Using vis studio 2012. And this is just one procedure in a program to convert input into an ascii chart with decimal, hex and ascii output. I've been trying to figure this out for 8 hours, and I know it's going to be something really simple.
It gets through all the computation, but during the pop and return phase it crashes into an unhandled exception when accessing the EIP(i think). Also, all my registers are set to 0 except ebx and I don't know why, but it may have something to do with it.
This is just the procedure to convert from the input string to a decimal value.*
My inputStr is:
inputStr db 16 DUP(0)
.code
main proc
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
lea esi, outputStr1
call PrintString
lea esi, inputStr
call GetString
call StrtoNum
invoke ExitProcess, 0 ;****This is the next line when it crashes***
main endp
StrtoNum proc ;going to hex first
pushad
pushfd
mov bl, 1 ;mov 1 into bl for later multiplying
whilemorechar:
mov al,byte ptr [esi]
cmp al, 0 ;stuff
je ConvertDone ;if null then we are done here
;esi is pointing to the string to be converted
;cmp bl,0
;jnz decrement
cmp al, 0h
je ConvertDec
sub al, 30h ;get first string byte to dec number 0-9
push ax ;push last digit to stack
inc esi ;gets to next string byte
inc cl ;make note of decimal position in string
jmp whilemorechar ;jump for next place in string
ConvertDec: ;reverse is done, now turn into decimal number
cmp cl, 0 ;compare counter to 0
jz ConvertDone ;if counter is 0, start comparing numbers
pop ax ;pop last on stack
mul bl ;multiply place value by input byte
add dx, ax ;add decimal value into dl
mov al, 10d ;move 10 into al
mul bx ;multiply 10 and bl value to get next
mov bx, ax ;mov decimal place into bl for next loop
dec cl ;decrement counter
jmp ConvertDec ;loop through again for next decimal place
ConvertDone:
mov ebx, 0
popfd ;pop flags
popad ;pop registers
ret ;return to caller
StrtoNum endp

Printing values of arrays in MASM assembly

I'm new to writing assembly code and I'm having trouble printing out the values of my array using a loop. The code I have prints out the value of the counter and the not the values in the array, can someone please explain what I'm doing wrong, also how do I point to the top of the array? I have tried using different registers, but nothing seems to be working. My professor asks that I do it this way (if it seems inefficient):
.386
.model flat
ExitProcess PROTO NEAR32 stdcall, dwExitCode:dword
Include io.h
cr equ 0DH
Lf equ 0AH
.stack 4096
.data
newline byte CR, LF, 0
whitespace byte 32,32,0
arr dword 10 dup(?)
n dword 2
string byte 40 dup(?)
prompt byte "Please enter a value: ", 0
origArr byte "Original Array", 0
.code
_start:
mov ecx,n ; number of values in the array
lea ebx,arr ; address of the array
sub edi, edi
top: cmp ecx, 0
je done
output prompt
input string, 40
atod string
mov [arr+edi], ecx
add edi, 4
loop top
done: output origArr
mov ecx, n
call myproc
INVOKE ExitProcess, 0
PUBLIC _start
myproc proc near32
.data
val_str byte 11 dup(?), 0
.code
push eax
push edi
push ecx
sub edi,edi ; index register
top2: mov eax, [ebx+edi]
dtoa val_str, eax
output val_str
add edi,4 ; modify esi rather than ebx
loop top2
pop ecx
pop edi
pop eax
ret
myproc endp
END
Any suggestions are appreciated.
mov [arr+edi], ecx
You're storing the loop counter, rather than the return value of atod.

Resources