RosettaCodeData/Task/String-length/X86-64-Assembly/string-length-2.x86-64
2026-04-30 12:34:36 -04:00

182 lines
4.8 KiB
Text

; compute string length
; assembly X86 window
; download and install visual studio 2022 free site microsoft
; search and open X64 native tools command prompt
; compil and link program with this command :
; ml64 lenString.asm /link /ENTRY:main /SUBSYSTEM:console kernel32.lib user32.lib Shell32.lib
; this program respects the 64-bit calling convention :
; registers arguments : rcx rdx r8 r9 and stack
; registers saved : rbx,rbp,rdi,rsi, r12-r15
; ATTENTION les registres rax,rcx,rdx,r8-r11 peuvent être
; perdus lors d'un appel de fonction
;*********************************
; constantes
;*********************************
STD_OUTPUT_HANDLE equ -11
BUFFERSIZE equ 200
;*********************************
; macros
;*********************************
;for this file, see task include a file for X86-64 language
include macrosInclude.asm
;*********************************
; user data
;*********************************
.data
szMesslength db "The lenght of string ",0
LGMESSLEN equ $ - szMesslength - 1
szMesslength1 db " is ",0
LGMESSLEN1 equ $ - szMesslength1 - 1
szCarriageReturn db 10,0
szString1 db "Apple",0
LGSTRING1 equ $ - szString1 - 1
szString2 db "Orange",0
LGSTRING2 equ $ - szString2 - 1
align 8
hConsole dq 0
sConvArea db 24 dup (0)
sBuffer db BUFFERSIZE dup (' ')
;*********************************
; user code fonction principale
;*********************************
.code
extern WriteFile : proc, GetStdHandle : proc, ExitProcess : proc
extern GetLastError : proc
main PROC public
displayLib "Program X86-64 start."
mov rcx, offset szMesslength
mov rdx,LGMESSLEN
call afficherConsole
mov rcx, offset szString1
mov rdx,LGSTRING1
call afficherConsole
mov rcx, offset szMesslength1
mov rdx,LGMESSLEN1
call afficherConsole
mov rcx, offset szString1
call lenString ; compute length
mov rcx,rax
mov rdx, offset sConvArea
call conversion10
mov rdx,rax ; result size
mov rcx, offset sConvArea
call afficherConsole
mov rcx, offset szCarriageReturn
mov rdx,1
call afficherConsole
mov rcx, offset szMesslength
mov rdx,LGMESSLEN
call afficherConsole
mov rcx, offset szString1
mov rdx,LGSTRING1
call afficherConsole
mov rcx, offset szMesslength1
mov rdx,LGMESSLEN1
call afficherConsole
mov rcx, offset szString1
call lenString2 ; other compute methode
mov rcx,rax
mov rdx, offset sConvArea
call conversion10
mov rdx,rax ; result size
mov rcx, offset sConvArea
call afficherConsole
mov rcx, offset szCarriageReturn
mov rdx,1
call afficherConsole
mov rcx, offset szMesslength
mov rdx,LGMESSLEN
call afficherConsole
mov rcx, offset szString1
mov rdx,LGSTRING1
call afficherConsole
mov rcx, offset szMesslength1
mov rdx,LGMESSLEN1
call afficherConsole
mov rcx,LGSTRING1 ; direct value for static string
mov rdx, offset sConvArea
call conversion10
mov rdx,rax ; result size
mov rcx, offset sConvArea
call afficherConsole
mov rcx, offset szCarriageReturn
mov rdx,1
call afficherConsole
mov rcx, offset szMesslength
mov rdx,LGMESSLEN
call afficherConsole
mov rcx, offset szString2
mov rdx,LGSTRING2
call afficherConsole
mov rcx, offset szMesslength1
mov rdx,LGMESSLEN1
call afficherConsole
mov rcx, offset szString2
call lenString ; compute length
mov rcx,rax
mov rdx, offset sConvArea
call conversion10
mov rdx,rax ; result size
mov rcx, offset sConvArea
call afficherConsole
mov rcx, offset szCarriageReturn
mov rdx,1
call afficherConsole
finProgramme:
displayLib "Program end."
sub rsp,28h
mov rcx,0 ; return code
call ExitProcess
main ENDP
;***************************************************
;compute string length
;****************************************************
; rcx : string address
lenString PROC
mov rax,0 ; counter init
@B1:
mov dl,[rax+rcx] ; load one byte of string
cmp dl,0 ; zero final ?
je @B2 ; yes -> end
inc rax ; increment counter
jmp @B1 ; and loop
@B2:
ret
lenString ENDP
;***************************************************
;compute string length other method
;****************************************************
; rcx : string address
lenString2 PROC
push rdi
xor rax,rax ; raz rax
mov rdi,rcx ; move string address
mov rcx,-1 ; init rcx
cld ; direction flag to zero
repne scasb ; repeat comparaison one character
not rcx ; ???
dec rcx ; ???
mov rax,rcx ; return result
pop rdi
ret
lenString2 ENDP
;for this file, see task include a file for X86-64 language
include routinesInclude.asm
end