2013-05-16 20 views
2

I'm工作中,用戶輸入他的名字命名的程序,程序都應該小寫字母轉換爲大寫:臂組件Rasperry丕:將字符串轉換爲大寫

我使用%s格式讀取字符串:

.text 
ldr r0,=msj 
bl printf 
ldr r0,=format 
ldr r1,string 
bl scanf 



.data 
.align 2 
msj: .asciz "Enter you name: " 
format: .asciz "%s" 
string: .asciz "" 

我曾試圖從其減去32到每個字符,但我認爲字符串不以ASCII格式的數字。

有什麼辦法可以將整個單詞轉換爲大寫?

+0

您沒有爲'string'保留任何空間。它是一個單一的''\ 0''字符。嘗試'string:.fill 256,1,0'或者其他的東西。 –

回答

0

這是必不可少的算法:

for (int idx = 0; idx < len; ++idx) 
    if (str [idx] >= 'A' && str [idx] <= 'Z') 
     str [idx] += 'a' - 'A'; 

它有幾個部分你沒有。按字符掃描字符串。檢查大寫字母。添加(不減)小寫/大寫偏移量。

請注意,這通常不適用於Unicode。

+0

是的,這正是我所做的,但是當我再次打印字符串時,它不會返回大寫字母,它會返回隨機字符或只是一個空格 str [idx] + ='a' - 'A'; 我不認爲有彙編指令轉換爲大寫,通常我減法32當我有一個ascci字符(%c),但whith字符串(%s)它不會工作 –

+0

@PabloEstrada:嘗試添加32而不是減去。小寫字母比大寫字母具有更高的ASCII碼。此外,你沒有顯示所有相關的代碼。 – wallyk

2

這可能工作。目前我沒有任何ARM資料。

; call with address of string in 'R0'. 
upperString: 
1: ldrb r1,[r0],#1 
    tst r1  ; finished string with null terminator? 
    bxeq lr  ; then done and return 
    cmp r1,#'a' ; less than a? 
    blo 1b  ; then load next char. 
    cmp r1,#'z' ; greater than z? 
    bhi 1b  ; then load next char. 

    ; Value to upper case. 
    sub r1,r1,#('a' - 'A') ; subtract 32. 
    strb r1,[r0,#-1] ; put it back to memory. 
    b 1b  ; next character. 

至少這是一個很好的起點。這就像wallyk's代碼,除了我假設以空字符結尾的字符串而不是pascal字符串。要調用它,

ldr r0,=string 
    bl upperString